Blain Smith has uploaded this change for review.
container/ring: add ring example
Change-Id: Idd7e89f115da69853d497bf278dd24af8696a9c4
---
A src/container/ring/example_test.go
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/src/container/ring/example_test.go b/src/container/ring/example_test.go
new file mode 100644
index 0000000..eee9135
--- /dev/null
+++ b/src/container/ring/example_test.go
@@ -0,0 +1,45 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ring_test
+
+import (
+ "container/ring"
+ "fmt"
+ "strconv"
+)
+
+func Example() {
+ // Create 2 new rings.
+ r10 := ring.New(10)
+ r5 := ring.New(5)
+
+ // Fill r10 with numbers 1 to 10
+ for i := 1; i <= r10.Len(); i++ {
+ r10.Value = i
+ r10 = r10.Next()
+ }
+
+ // Fill r5 with string numbers "Num: 11" to "Num: 15"
+ for i := 1; i <= r5.Len(); i++ {
+ r5.Value = fmt.Sprintf("Num: %s", strconv.Itoa(i+10))
+ r5 = r5.Next()
+ }
+
+ r10 = r10.Move(2) // r10 points to Value: 3
+ r5 = r5.Move(4) // r5 points to Value: "Num: 15"
+
+ // Link the 2 rings together. r5 will get appended to r10 and the
+ // new ring will point to the next position (Value: 4) of the append
+ // point.
+ r15 := r10.Link(r5) // r15 points to Value: 4
+
+ for i := 1; i <= r15.Len(); i++ {
+ fmt.Printf("%v, ", r15.Value)
+ r15 = r15.Next()
+ }
+
+ // Output:
+ // 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, Num: 15, Num: 11, Num: 12, Num: 13, Num: 14,
+}
To view, visit change 49431. To unsubscribe, visit settings.
Brad Fitzpatrick posted comments on this change.
Patch set 1:
R=go1.10
(1 comment)
File src/container/ring/example_test.go:
Patch Set #1, Line 1: // Copyright 2013 The Go Authors. All rights reserved.
wrong year
To view, visit change 49431. To unsubscribe, visit settings.
Blain Smith uploaded patch set #2 to this change.
container/ring: add ring example
Change-Id: Idd7e89f115da69853d497bf278dd24af8696a9c4
---
A src/container/ring/example_test.go
1 file changed, 45 insertions(+), 0 deletions(-)
To view, visit change 49431. To unsubscribe, visit settings.
Blain Smith posted comments on this change.
Patch set 2:
Patch Set 1:
(1 comment)
R=go1.10
Copy and paste mistake.
Didier Spezia posted comments on this change.
Patch set 2:Code-Review -1
I have nothing against adding more examples, but they should not promote bad programming practices. Perhaps a better example can be found ...
(3 comments)
File src/container/ring/example_test.go:
Patch Set #2, Line 19: for i := 1; i <= r10.Len(); i++ {
Len is O(n) - you have just turned a nice linear algorithm into a quadratic one.
Patch Set #2, Line 25: for i := 1; i <= r5.Len(); i++ {
Ditto
Patch Set #2, Line 38: for i := 1; i <= r15.Len(); i++ {
Ditto
To view, visit change 49431. To unsubscribe, visit settings.
Blain Smith uploaded patch set #3 to this change.
container/ring: add ring example
Change-Id: Idd7e89f115da69853d497bf278dd24af8696a9c4
---
A src/container/ring/example_test.go
1 file changed, 47 insertions(+), 0 deletions(-)
To view, visit change 49431. To unsubscribe, visit settings.
Blain Smith posted comments on this change.
Patch set 3:
Patch Set 2: Code-Review-1
(3 comments)
I have nothing against adding more examples, but they should not promote bad programming practices. Perhaps a better example can be found ...
Good points. I updated the patched with the same basic example, but removed .Len() usage.
Didier Spezia posted comments on this change.
Patch set 3:Code-Review +1
LGTM
Ian Lance Taylor abandoned this change.
To view, visit change 49431. To unsubscribe, or for help writing mail filters, visit settings.