Skip to content

Instantly share code, notes, and snippets.

@chadlung
Last active May 27, 2016 01:37
Show Gist options
  • Save chadlung/755401a64910125cca8f67b695c61c4d to your computer and use it in GitHub Desktop.
Save chadlung/755401a64910125cca8f67b695c61c4d to your computer and use it in GitHub Desktop.
Introducing a race condition with Go
package main
import (
"fmt"
)
func main() {
// This code introduces a race condition!!!
finished := make(chan bool, 1)
var i int
go func() {
fmt.Println("In the Go routine reading to increment i")
i++
fmt.Println("New value of i:", i)
finished <- true
}()
i++
<-finished
fmt.Println("Finished!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment