Last active
September 14, 2017 23:12
-
-
Save hibooboo2/d8296b0fb2e3481466eb3ad3f7725971 to your computer and use it in GitHub Desktop.
Shuffle any thing golang. No funcs. Simple.
This file contains hidden or 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 | |
func main() { | |
items := map[string]interface{}{ | |
"boom": "man", | |
"sweet": "awesome", | |
"ok": "another", | |
"able": "able", | |
"about": "about", | |
"above": "above", | |
"abroad": "abroad", | |
"absence": "absence", | |
"absent": "absent", | |
"absolute": "absolute", | |
"absolutely": "absolutely", | |
"absorb": "absorb", | |
"abuse": "abuse", | |
"academic": "academic", | |
"accent": "accent", | |
"accept": "accept", | |
} | |
s := NewShuffler(items) | |
defer s.Close() | |
for i := 0; i < 342; i++ { | |
println(s.Random().(string)) | |
} | |
} | |
type Shuffler struct { | |
items map[string]interface{} | |
itemChan chan interface{} | |
} | |
func (s *Shuffler) Random() interface{} { | |
return <-s.itemChan | |
} | |
func NewShuffler(items map[string]interface{}) *Shuffler { | |
s := &Shuffler{ | |
items: make(map[string]interface{}), | |
itemChan: make(chan interface{}), | |
} | |
for k, v := range items { | |
s.items[k] = v | |
} | |
go func() { | |
for { | |
for _, v := range s.items { | |
s.itemChan <- v | |
} | |
} | |
}() | |
return s | |
} | |
func (s *Shuffler) Close() { | |
close(s.itemChan) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment