From bc1d76a796b53425f51b546f21eeafdcb175bfba Mon Sep 17 00:00:00 2001 From: mingzaily Date: Wed, 3 Nov 2021 11:41:34 +0800 Subject: [PATCH 1/4] add example for gqueue --- container/gqueue/gqueue_z_example_test.go | 115 ++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 container/gqueue/gqueue_z_example_test.go diff --git a/container/gqueue/gqueue_z_example_test.go b/container/gqueue/gqueue_z_example_test.go new file mode 100644 index 000000000..f3f414fb7 --- /dev/null +++ b/container/gqueue/gqueue_z_example_test.go @@ -0,0 +1,115 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gqueue_test + +import ( + "context" + "fmt" + "github.com/gogf/gf/v2/container/gqueue" + "github.com/gogf/gf/v2/os/gtimer" + "time" +) + +func ExampleNew() { + n := 10 + q := gqueue.New() + + // Producer + for i := 0; i < n; i++ { + q.Push(i) + } + + fmt.Println(q.Len()) + + // Close the queue in three seconds. + gtimer.SetTimeout(context.Background(), time.Second*3, func(ctx context.Context) { + q.Close() + }) + + // The consumer constantly reads the queue data. + // If there is no data in the queue, it will block. + // The queue is read using the queue.C property exposed + // by the queue object and the selectIO multiplexing syntax + // example: + // for { + // select { + // case v := <-queue.C: + // if v != nil { + // fmt.Println(v) + // } else { + // return + // } + // } + // } + for { + if v := q.Pop(); v != nil { + fmt.Print(v) + } else { + break + } + } + + // Output: + // 10 + // 0123456789 +} + +func ExampleQueue_Push() { + q := gqueue.New() + + for i := 0; i < 10; i++ { + q.Push(i) + } + + fmt.Println(q.Len()) + + // Output: + // 10 +} + +func ExampleQueue_Pop() { + q := gqueue.New() + + for i := 0; i < 10; i++ { + q.Push(i) + } + + fmt.Println(q.Pop()) + q.Close() + fmt.Println(q.Pop()) + + // Output: + // 0 + // +} + +func ExampleQueue_Close() { + q := gqueue.New() + + for i := 0; i < 10; i++ { + q.Push(i) + } + + q.Close() + + fmt.Println(q.Pop()) + + // Output: + // +} + +func ExampleQueue_Len() { + q := gqueue.New() + + q.Push(1) + q.Push(2) + + fmt.Println(q.Len()) + + // Output: + // 2 +} From 9c05682605a9126af1c47f23caa2bed4c8dc93a9 Mon Sep 17 00:00:00 2001 From: mingzaily Date: Wed, 3 Nov 2021 16:16:03 +0800 Subject: [PATCH 2/4] change some example for gqueue --- container/gqueue/gqueue_z_example_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/container/gqueue/gqueue_z_example_test.go b/container/gqueue/gqueue_z_example_test.go index f3f414fb7..ba99c51d9 100644 --- a/container/gqueue/gqueue_z_example_test.go +++ b/container/gqueue/gqueue_z_example_test.go @@ -94,11 +94,14 @@ func ExampleQueue_Close() { q.Push(i) } + time.Sleep(time.Millisecond) q.Close() + fmt.Println(q.Len()) fmt.Println(q.Pop()) // Output: + // 0 // } @@ -108,8 +111,13 @@ func ExampleQueue_Len() { q.Push(1) q.Push(2) + time.Sleep(time.Millisecond) + fmt.Println(q.Len()) + q.Pop() + q.Close() fmt.Println(q.Len()) // Output: // 2 + // 0 } From 38b797b42fbd5a454d396a634ae2a15c56f09786 Mon Sep 17 00:00:00 2001 From: mingzaily Date: Sat, 6 Nov 2021 16:27:17 +0800 Subject: [PATCH 3/4] finish example for gqueue --- container/gqueue/gqueue_z_example_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/container/gqueue/gqueue_z_example_test.go b/container/gqueue/gqueue_z_example_test.go index ba99c51d9..119b23d2c 100644 --- a/container/gqueue/gqueue_z_example_test.go +++ b/container/gqueue/gqueue_z_example_test.go @@ -111,13 +111,8 @@ func ExampleQueue_Len() { q.Push(1) q.Push(2) - time.Sleep(time.Millisecond) - fmt.Println(q.Len()) - q.Pop() - q.Close() fmt.Println(q.Len()) // Output: // 2 - // 0 } From df02ca764fffb64898ef75663248afe9dbc53243 Mon Sep 17 00:00:00 2001 From: mingzaily Date: Sun, 7 Nov 2021 16:52:28 +0800 Subject: [PATCH 4/4] add size example for gqueue --- container/gqueue/gqueue_z_example_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/container/gqueue/gqueue_z_example_test.go b/container/gqueue/gqueue_z_example_test.go index 119b23d2c..cb2b4696d 100644 --- a/container/gqueue/gqueue_z_example_test.go +++ b/container/gqueue/gqueue_z_example_test.go @@ -116,3 +116,16 @@ func ExampleQueue_Len() { // Output: // 2 } + +func ExampleQueue_Size() { + q := gqueue.New() + + q.Push(1) + q.Push(2) + + // Size is alias of Len. + fmt.Println(q.Size()) + + // Output: + // 2 +}