-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
29 lines (26 loc) · 843 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main
import "fmt"
// main demonstrates one of the core go synchronisation
// primitves, the channel. There is a lot of complexity
// to channels in go, see the `channels` documentation in
// this repository for a deep dive.
//
// This example fixes the race condition in the hello_world_goroutine
// example.
func main() {
/*
We make an (unbuffered) channel, one with no length/cap.
After the spawned goroutine is finished, we close the channel
which causes the receive <- on it to finally be unblocked.
*/
c := make(chan struct{}) // We use struct{} as it uses zero memory.
go func() {
saySomething("foo")
close(c)
}()
<-c // This will block until the channel is closed, causing the program not to exit.
}
// saySomething prints a simple phrase to stdout.
func saySomething(phrase string) {
fmt.Println(phrase)
}