diff --git a/container/gqueue/gqueue.go b/container/gqueue/gqueue.go index de2a06504..a778709b3 100644 --- a/container/gqueue/gqueue.go +++ b/container/gqueue/gqueue.go @@ -59,7 +59,7 @@ func New(limit ...int) *Queue { } // asyncLoopFromListToChannel starts an asynchronous goroutine, -// which handles the data synchronization from list to channel . +// which handles the data synchronization from list `q.list` to channel `q.C`. func (q *Queue) asyncLoopFromListToChannel() { defer func() { if q.closed.Val() { @@ -87,13 +87,13 @@ func (q *Queue) asyncLoopFromListToChannel() { <-q.events } } - // It should be here to close q.C if `q` is unlimited size. + // It should be here to close `q.C` if `q` is unlimited size. // It's the sender's responsibility to close channel when it should be closed. close(q.C) } // Push pushes the data `v` into the queue. -// Note that it would panics if Push is called after the queue is closed. +// Note that it would panic if Push is called after the queue is closed. func (q *Queue) Push(v interface{}) { if q.limit > 0 { q.C <- v @@ -121,14 +121,15 @@ func (q *Queue) Close() { } if q.limit > 0 { close(q.C) - } - for i := 0; i < defaultBatchSize; i++ { - q.Pop() + } else { + for i := 0; i < defaultBatchSize; i++ { + q.Pop() + } } } // Len returns the length of the queue. -// Note that the result might not be accurate as there's a +// Note that the result might not be accurate as there's an // asynchronous channel reading the list constantly. func (q *Queue) Len() (length int) { if q.list != nil { diff --git a/container/gqueue/gqueue_z_example_test.go b/container/gqueue/gqueue_z_example_test.go index cb2b4696d..d561335ee 100644 --- a/container/gqueue/gqueue_z_example_test.go +++ b/container/gqueue/gqueue_z_example_test.go @@ -65,10 +65,14 @@ func ExampleQueue_Push() { q.Push(i) } - fmt.Println(q.Len()) + fmt.Println(q.Pop()) + fmt.Println(q.Pop()) + fmt.Println(q.Pop()) // Output: - // 10 + // 0 + // 1 + // 2 } func ExampleQueue_Pop() { @@ -113,7 +117,7 @@ func ExampleQueue_Len() { fmt.Println(q.Len()) - // Output: + // May Output: // 2 } @@ -126,6 +130,6 @@ func ExampleQueue_Size() { // Size is alias of Len. fmt.Println(q.Size()) - // Output: + // May Output: // 2 }