github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/examples/MapCrash/MapCrash.go (about) 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 var m map[string]string 8 var comm chan int 9 10 func initmap() { 11 m = make(map[string]string) 12 comm <- 1 13 } 14 15 func reader() { 16 for i := 0; i <= 5; i++ { 17 for k, v := range m { 18 fmt.Println(k,v) 19 } 20 } 21 comm <- 2 22 } 23 24 func writer() { 25 for i := 0; i <= 5; i++ { 26 m[string(i)] = string(i) 27 } 28 comm <- 3 29 } 30 31 func main() { 32 // Initializes the communication channel between the main 33 // goroutine and the children goroutines 34 comm = make(chan int, 3) 35 // Launches a goroutine that initializes the shared map 36 go initmap() 37 // Launches a goroutine that reads from the shared map 38 go reader() 39 // Launches a goroutine that writes to the shared map 40 go writer() 41 <- comm 42 <- comm 43 <- comm 44 }