Using Go's select Statement

If you are an experience Go developer this will likely all be review, but I wanted to share it since I am working on a more in-depth tutorial that requires a reasonable understanding of channels and Go’s select statement.

Let’s start with the basics - imagine that you have a channel named someChannel and you are waiting on a signal from that channel. In Go we can do this with the following code.

// Waiting for a message from a single channel.
<-someChannel

If we want the value sent over the channel, we can assign it to a variable.

value := <-someChannel

Quite frequently, we will want to tell our program to wait for a message from one of multiple channels. What we basically want is a switch statement that can wait for a message from one of multiple channels.

// This is NOT valid Go code.
switch {
case <-someChannel:
  // ...
case <-someOtherChannel:
  // ...
}

We can do this using the select statement, which looks very similar to the switch statement, but is tailored specifically for waiting for messages from multiple channels.

select {
case <-someChannel:
  // ...
case <-someOtherChannel:
  // ...
}

When we use the select statement, our program will block until one of the channels has a message, at which point the select statement will execute the corresponding case block. We can see a complete example in the following code.


package main

import (
	"fmt"
	"time"
)

func main() {
	chA, chB := make(chan int), make(chan int)

	go func() {
		time.Sleep(1 * time.Second)
		chA <- 2
	}()
	go func() {
		time.Sleep(2 * time.Second)
		chB <- 3
	}()

	select {
	case a := <-chA:
		fmt.Println("A", a)
	case b := <-chB:
		fmt.Println("B", b)
	}
}

In this example we can also see another unique difference between the select statement and the switch statement - the assignment of values to a new variable in a case block. Specifically, if we receive a message from either channel we can assign it to a new variable and use that variable in the case block.

case value := <-someChannel:
  // use value here...

While the code here is fairly simple, especially if one is already familiar with Go’s select statement, it can enable some very powerful patterns in Go programs.

Learn Web Development with Go!

Sign up for my mailing list and I'll send you a FREE sample from my course - Web Development with Go. The sample includes 19 screencasts and the first few chapters from the book.

You will also receive emails from me about Go coding techniques, upcoming courses (including FREE ones), and course discounts.

    Avatar of Jon Calhoun
    Written by
    Jon Calhoun

    Jon Calhoun is a full stack web developer who teaches about Go, web development, algorithms, and anything programming. If you haven't already, you should totally check out his Go courses.

    Previously, Jon worked at several statups including co-founding EasyPost, a shipping API used by several fortune 500 companies. Prior to that Jon worked at Google, competed at world finals in programming competitions, and has been programming since he was a child.

    Related articles

    Spread the word

    Did you find this page helpful? Let others know about it!

    Sharing helps me continue to create both free and premium Go resources.

    Want to discuss the article?

    See something that is wrong, think this article could be improved, or just want to say thanks? I'd love to hear what you have to say!

    You can reach me via email or via twitter.

    Recent Articles All Articles Mini-Series Progress Updates Tags About Me Go Courses

    ©2024 Jonathan Calhoun. All rights reserved.