diff --git a/g/container/gchan/gchan.go b/g/container/gchan/gchan.go index 4c346fc7b..69bcab9f8 100644 --- a/g/container/gchan/gchan.go +++ b/g/container/gchan/gchan.go @@ -4,13 +4,13 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// 优雅的channel操作封装 +// 优雅的channel操作. package gchan import ( "sync" - "sync/atomic" "errors" + "sync/atomic" ) type Chan struct { @@ -28,7 +28,7 @@ func New(limit int) *Chan { // 将数据压入队列 func (q *Chan) Push(v interface{}) error { if atomic.LoadInt32(&q.closed) > 0 { - return errors.New("channel closed") + return errors.New("closed") } q.list <- v return nil diff --git a/g/container/gqueue/gqueue.go b/g/container/gqueue/gqueue.go index bf1a9e44a..b27e3b15a 100644 --- a/g/container/gqueue/gqueue.go +++ b/g/container/gqueue/gqueue.go @@ -5,14 +5,17 @@ // You can obtain one at https://gitee.com/johng/gf. // 动态大小的安全队列(dynamic channel). +// 优点: +// 1、队列初始化速度快; +// 2、可以向队头/队尾进行Push/Pop操作; package gqueue import ( "math" "sync" - "container/list" - "sync/atomic" "errors" + "sync/atomic" + "container/list" ) type Queue struct { @@ -40,7 +43,7 @@ func New(limit...int) *Queue { // 将数据压入队列, 队尾 func (q *Queue) PushBack(v interface{}) error { if atomic.LoadInt32(&q.closed) > 0 { - return errors.New("queue closed") + return errors.New("closed") } if q.limit > 0 { q.limits <- struct{}{} @@ -55,7 +58,7 @@ func (q *Queue) PushBack(v interface{}) error { // 将数据压入队列, 队头 func (q *Queue) PushFront(v interface{}) error { if atomic.LoadInt32(&q.closed) > 0 { - return errors.New("queue closed") + return errors.New("closed") } if q.limit > 0 { q.limits <- struct{}{} @@ -104,7 +107,7 @@ func (q *Queue) PopBack() interface{} { return nil } -// 关闭队列(通知所有通过Pop阻塞的协程退出) +// 关闭队列(通知所有通过Pop*阻塞的协程退出) func (q *Queue) Close() { if atomic.LoadInt32(&q.closed) == 0 { atomic.StoreInt32(&q.closed, 1) diff --git a/geg/other/test.go b/geg/other/test.go index 5e842e6a1..78fdb6bb7 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,12 +1,15 @@ package main -import ( - "time" -) func main() { + defer func() { + recover() + }() events1 := make(chan int, 100) - events2 := make(chan int, 100) + events1 <- 1 + close(events1) + events1 <- 2 + //events2 := make(chan int, 100) //go func() { // for{ // v := <- events1 @@ -15,18 +18,18 @@ func main() { // //}() - go func() { - time.Sleep(2*time.Second) - events1 <- 1 - events2 <- 2 - time.Sleep(2*time.Second) - close(events1) - close(events2) - events1 <- 1 - events2 <- 2 - }() - - select { - - } + //go func() { + // time.Sleep(2*time.Second) + // events1 <- 1 + // events2 <- 2 + // time.Sleep(2*time.Second) + // close(events1) + // close(events2) + // events1 <- 1 + // events2 <- 2 + //}() + // + //select { + // + //} } \ No newline at end of file