The following excerpt is from “The Go Memory Model” (at The Go Memory Model - The Go Programming Language):
A receive from an unbuffered channel happens before the send on that channel completes.
This program (as above, but with the send and receive statements swapped and using an unbuffered channel):
var c = make(chan int)
var a string
func f() {
a = "hello, world"
<-c
}
func main() {
go f()
c <- 0
print(a)
}
is also guaranteed to print “hello, world”. The write to a happens before the receive on c, which happens before the corresponding send on c completes, which happens before the print.
If the channel were buffered (e.g., c = make(chan int, 1)) then the program would not be guaranteed to print “hello, world”. (It might print the empty string, crash, or do something else.)
The last (parenthetical) sentence indicates that the program might crash or “do something else”. However, why would this be? I see only two possibilities:
- The goroutine “f” manages to quickly exit, changing the value of variable “a” before the “print” command (main) executes–thereby printing “hello, world”.
- The goroutine is “slow”, and main() prints an empty string and exits.
Could someone shed some light on why the article says that the program might crash or “do something else”?