-
Notifications
You must be signed in to change notification settings - Fork 0
/
generategophers.go
75 lines (65 loc) · 1.25 KB
/
generategophers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bufio"
"encoding/gob"
"log"
"os"
"strconv"
"sync"
"sync/atomic"
"github.com/coip/underlying-gobs/lib/gopher"
)
var (
names = []string{
"Mallory",
"Willa",
"Maurise",
"Bernete",
"Carly",
"Evered",
"Guss",
"Jobyna",
"Jena",
"Stinky",
"Sybila",
"Angie",
"Bryn",
"Emmalyn",
}
)
var population = int32(0)
var poplock sync.Mutex
// FROM https://blog.klauspost.com/defer-fun/
func Un(f func()) {
f()
}
func Lock(x sync.Locker) func() {
x.Lock()
return func() { x.Unlock() }
}
///////////////////////////
func main() {
seedthought, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
os.Exit(1)
}
seedthought = seedthought[:len(seedthought)-1]
gd := gob.NewEncoder(os.Stdout)
targetPopulation := os.Args[1]
tmpnum, e := strconv.Atoi(targetPopulation)
if e != nil || tmpnum < 1 {
os.Stderr.Write([]byte("supply a real target population betch\n"))
os.Exit(1)
}
for i := 0; i < tmpnum; i++ {
f := Lock(&poplock)
g := gopher.MakeGopher(names[population], seedthought, population)
atomic.AddInt32(&population, 1)
Un(f)
e := gd.Encode(g)
if e != nil {
log.Printf("Err: %+v", e)
}
}
os.Exit(0)
}