Quick actions

cmd+k|ctrl+k

Navigation

Languages

Channels and select / Closing channels / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:15:11Z

Updated

2019-03-26T10:15:11Z

package main

import (
	"fmt"
	"time"
)

func main() {
	ch := make(chan string)

	go func() {
		for s := range ch {
			fmt.Printf("received from channel: %s\n", s)
		}
		fmt.Print("range loop finished because ch was closed\n")
	}()

	ch <- "foo"
	close(ch)

	// only to simplify example, don't sleep to coordinate
	// goroutines in real code
	time.Sleep(100 * time.Millisecond)
}
INFO