Skip to content

Instantly share code, notes, and snippets.

@codeskyblue
Created January 18, 2014 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeskyblue/8485670 to your computer and use it in GitHub Desktop.
Save codeskyblue/8485670 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"time"
)
type Record struct {
a int
b int
c int
}
var (
m = make(map[string]Record)
v1 = Record{1, 2, 3}
v2 = Record{4, 5, 6}
empty = Record{0, 0, 0}
)
func Check(m map[string]Record) {
x := m["a"]
if x != v1 && x != v2 && x != empty {
log.Fatal("but got expected", x)
}
}
func main() {
fmt.Println("Hello world")
m["a"] = v1
go func() {
for {
m["a"] = v2
Check(m)
}
}()
go func() {
for {
m["a"] = v1
Check(m)
}
}()
go func() {
for {
delete(m, "a")
m["a"] = v1
Check(m)
}
}()
for {
time.Sleep(time.Second)
}
}
@codeskyblue
Copy link
Author

when call delete function, other go routine may got nil of m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment