Don't automatically do concurrency for users
http://talks.golang.org/2013/bestpractices.slide#25
This commit is contained in:
parent
0d11b8a0ba
commit
8b52f93ca5
13
id.go
13
id.go
@ -18,15 +18,16 @@ type IdGenerator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewIdGenerator() *IdGenerator {
|
func NewIdGenerator() *IdGenerator {
|
||||||
g := IdGenerator{}
|
return &IdGenerator{
|
||||||
g.id = make(chan int64)
|
id: make(chan int64),
|
||||||
go func() {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (idg *IdGenerator) Run() {
|
||||||
var i int64
|
var i int64
|
||||||
for i = 0; ; i++ {
|
for i = 0; ; i++ {
|
||||||
g.id <- i
|
idg.id <- i
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
return &g
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (id *IdGenerator) Hash() string {
|
func (id *IdGenerator) Hash() string {
|
||||||
|
@ -8,6 +8,7 @@ func TestIDGenerator(t *testing.T) {
|
|||||||
cache := make(map[string]bool)
|
cache := make(map[string]bool)
|
||||||
var cur string
|
var cur string
|
||||||
gg := NewIdGenerator()
|
gg := NewIdGenerator()
|
||||||
|
go gg.Run()
|
||||||
for i := 0; i < 10000; i++ {
|
for i := 0; i < 10000; i++ {
|
||||||
cur = gg.Hash()
|
cur = gg.Hash()
|
||||||
if _, ok := cache[cur]; ok {
|
if _, ok := cache[cur]; ok {
|
||||||
|
Loading…
Reference in New Issue
Block a user