Created
December 24, 2013 00:47
-
-
Save fatih/8107159 to your computer and use it in GitHub Desktop.
container/ring example golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"container/ring" | |
"fmt" | |
"time" | |
) | |
func main() { | |
coffee := []string{"kenya", "guatemala", "ethiopia"} | |
// create a ring and populate it with some values | |
r := ring.New(len(coffee)) | |
for i := 0; i < r.Len(); i++ { | |
r.Value = coffee[i] | |
r = r.Next() | |
} | |
// print all values of the ring, easy done with ring.Do() | |
r.Do(func(x interface{}) { | |
fmt.Println(x) | |
}) | |
// .. or each one by one. | |
for _ = range time.Tick(time.Second * 1) { | |
r = r.Next() | |
fmt.Println(r.Value) | |
} | |
} |
Another play link (exits after 10 seconds to avoid play.golang.org error).
http://play.golang.org/p/1acRTTh6ZF
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
play link: http://play.golang.org/p/e5-R7nZX_P