×

go · level 3 · unit 2 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A is a pipe that carries values between goroutines.

A channel carries values of one type between goroutines. Make one with make(chan string). Send a value into it with ch <- "ready", and receive one with msg := <-ch. The arrow always points the way the value travels.

A channel made like this is : it has no room to store anything. A send waits until another goroutine receives, and a receive waits until someone sends. They meet, hand over and both carry on. Go programmers call this waiting , which has nothing to do with a block of code.

Because a receive waits, a channel can stand in for a WaitGroup when you need one answer back: main receives, and cannot go on until the goroutine has sent.

package main
import "fmt"
func brew(order string, ready chan string) {
	ready <- order + " is ready"
}
func main() {
	ready := make(chan string)
	go brew("flat white", ready)
	fmt.Println(<-ready)
}

<-ready is a value like any other, so you can print it, store it or add it up.