Your results make sense. Since go run run is independent of each other, you will never know when the normal procedure starts. As soon as the line
m[2] = "Second Value"
Performed
this will affect your normal procedure.
Therefore, when the first and second printouts of your program are performed on the line, you see the result as
1-First Value 2-Second Value 3-Second Value 4-Second Value
When you do not see another. Before the third print, you will be sure that the normal procedure is complete.
Just clean it even more if you change your program a bit like
package main import "fmt" import "time" func main() { m := make(map[int]string) m[2] = "First Value" c := make(chan bool) go func() { m[2] = "Second Value" c <- true }() time.Sleep(time.Second) fmt.Printf("1-%s\n", m[2]) fmt.Printf("2-%s\n", m[2]) _ = <-c fmt.Printf("3-%s\n", m[2]) fmt.Printf("4-%s\n", m[2]) }
Playground
Most likely you will get the following output
1-Second Value 2-Second Value 3-Second Value 4-Second Value
Hope this helps.
source share