28-nilChannels

command
v0.0.0-...-9882766 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Example is for testing out setting channels to nil. Starting up 2 go routines who will produce a set of numbers, and put them on a channel for each go routine. When to go routine is done we close the channel.

We then make a loop with a select, and read all the values from the 2 channels. With OK we check if the channel is closed, and if it is closed we set the channel to nil. The interesting thing bout settign a channel to nil is that it will not be checked anymore in the select, and the case is ignored. Something that can be seen in this example that the print statement inside the case about setting the channel to nil is only printed once.

We then check that both channels are set to nil, and break out of the loop.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL