Language FeaturesGoChannels

Channels

Channels are how multiple Goroutines communicate with one another.

Creating a channel

package main
 
import "fmt"
 
func sum(s []int, c chan int) {
	sum := 0
	for _, v := range s {
		sum += v
	}
	c <- sum // send sum to c
}
 
func main() {
	s := []int{7, 2, 8, -9, 4, 0}
 
	c := make(chan int)
	go sum(s[:len(s)/2], c)
	go sum(s[len(s)/2:], c)
	x, y := <-c, 0 // receive from c
 
	fmt.Println(x, y, x+y)
}

select

package main
 
import "fmt"
 
func fibonacci(c, quit chan int) {
	x, y := 0, 1
	for {
		select {
		case c <- x:
			x, y = y, x+y
		case <-quit:
			fmt.Println("quit")
			return
		}
	}
}
 
func main() {
	c := make(chan int)
	quit := make(chan int)
	go func() {
		for i := 0; i < 10; i++ {
			fmt.Println(<-c)
		}
		quit <- 0
	}()
	fibonacci(c, quit)
}

A select statement allows a Goroutine to wait on multiple channels.