Golang program to demonstrate the channel with select statement

Here, we are going to demonstrate the channel with select statement in Golang (Go Language).
Submitted by Nidhi, on April 04, 2021

Problem Solution:

In this program, we will create two channels to store Boolean value. Here, we will send Boolean values to channels in user-defined function and then received values using select statement.

Program/Source Code:

The source code to demonstrate the channel with a select statement is given below. The given program is compiled and executed successfully.

// Golang program to demonstrate the channel
// with select statement

package main

import "fmt"

func SetChannels(chnl1 chan bool, chnl2 chan bool) {
	chnl1 <- true
	chnl1 <- false

func main() {

	channel1 := make(chan bool)
	channel2 := make(chan bool)

	go SetChannels(channel1, channel2)

	for i := 0; i < 2; i++ {
		select {
		case val1 := <-channel1:
			fmt.Println("Received value: ", val1)
		case val2 := <-channel2:
			fmt.Println("Received value: ", val2)


Received value:  true
Received value:  false


In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package to formatting related functions.

In the main() function, we created two channels. Then we send values to channels in SetChannels() function. After that, we received and printed values of channels on the console screen.

Golang Channels Programs »


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.