Created
January 9, 2016 22:47
-
-
Save ds0nt/3d64683ed3480b28793a to your computer and use it in GitHub Desktop.
rand.go
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 | |
import "fmt" | |
type hooray struct { | |
v []int | |
} | |
var ticker = 0 | |
var uhoh = hooray{make([]int, 0)} | |
func appendStuff(stuff int) { | |
uhoh.v = append(uhoh.v, stuff) | |
ticker++ | |
} | |
func dangerouslyWrite(done chan struct{}) { | |
i := 0 | |
for { | |
appendStuff(i) | |
i++ | |
if i > 1000 { | |
break | |
} | |
} | |
done <- struct{}{} | |
} | |
func rand(max int) int { | |
done := make(chan struct{}) | |
for index := 0; index < 10; index++ { | |
go dangerouslyWrite(done) | |
} | |
for index := 0; index < 10; index++ { | |
<-done | |
} | |
return max * ticker / 10000 | |
} | |
func main() { | |
x := rand(100) | |
fmt.Println(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment