Skip to content

Instantly share code, notes, and snippets.

@fatih
Created December 24, 2013 00:47
Show Gist options
  • Save fatih/8107159 to your computer and use it in GitHub Desktop.
Save fatih/8107159 to your computer and use it in GitHub Desktop.
container/ring example golang
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)
}
}
@fatih
Copy link
Author

fatih commented Mar 5, 2014

@thani-sh
Copy link

thani-sh commented Jun 5, 2015

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