DEV Community

Cover image for Go Course: Select
Karan Pratap Singh
Karan Pratap Singh

Posted on • Originally published at karanpratapsingh.com

2

Go Course: Select

In this tutorial, we will learn about the select statement in Go.

The select statement blocks the code and waits for multiple channel operations simultaneously.

A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

package main

import (
    "fmt"
    "time"
)

func main() {
    one := make(chan string)
    two := make(chan string)

    go func() {
        time.Sleep(time.Second * 2)
        one <- "One"
    }()

    go func() {
        time.Sleep(time.Second * 1)
        two <- "Two"
    }()

    select {
    case result := <-one:
        fmt.Println("Received:", result)
    case result := <-two:
        fmt.Println("Received:", result)
    }

    close(one)
    close(two)
}
Enter fullscreen mode Exit fullscreen mode

Similar to switch , select also has a default case that runs if no other case is ready. This will help us send or receive without blocking.

func main() {
    one := make(chan string)
    two := make(chan string)

    for x := 0; x < 10; x++ {
        go func() {
            time.Sleep(time.Second * 2)
            one <- "One"
        }()

        go func() {
            time.Sleep(time.Second * 1)
            two <- "Two"
        }()
    }

    for x := 0; x < 10; x++ {
        select {
        case result := <-one:
            fmt.Println("Received:", result)
        case result := <-two:
            fmt.Println("Received:", result)
        default:
            fmt.Println("Default...")
            time.Sleep(200 * time.Millisecond)
        }
    }

    close(one)
    close(two)
}
Enter fullscreen mode Exit fullscreen mode

It's also important to know that an empty select {} blocks forever.

func main() {
    ...
    select {}

    close(one)
    close(two)
}
Enter fullscreen mode Exit fullscreen mode

This article is part of my open source Go Course available on Github.

GitHub logo karanpratapsingh / learn-go

Master the fundamentals and advanced features of the Go programming language

Learn Go

Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.

This course is also available on my website and as an ebook on leanpub. Please leave a ⭐ as motivation if this was helpful!

Table of contents

What is Go?

Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.

It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed…




Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series