Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine. Channels are a powerful primitive that underly much of Go’s functionality. |
|
package main
|
|
import "fmt"
|
|
func main() {
|
|
Create a new channel with |
messages := make(chan string)
|
Send a value into a channel using the |
go func() { messages <- "ping" }()
|
The |
msg := <-messages
fmt.Println(msg)
}
|
When we run the program the |
$ go run channels.go
ping
|
By default sends and receives block until both the
sender and receiver are ready. This property allowed
us to wait at the end of our program for the |
Next example: Channel Buffering.