Closing the Channel in Golang

Learn via video courses
Topics Covered

Overview

A channel in Go is a medium through which a goroutine communicates with another goroutine in a lock-free manner. In simple words, a channel is a mechanism that allows one goroutine to transfer data to another.

A channel is created in using the chan keyword or make() function, and it can only transfer data of the same type. Different types of data are not allowed to transport from the same channel. You can use the close() function to close a channel. This built-in function sets a flag indicating that no additional data will be sent to this channel. This article focuses only on how to close a channel in golang.

What does a Closed Channel Denote in Golang?

A channel in golang is a mechanism that allows one goroutine to transfer data to another. A closed channel states that we can't send data to it, but we can still read data from it. A closed channel signifies a circumstance in which we wish to demonstrate that the work on this channel has been completed, and there is no need for it to be open.

We don't have to close every channel when you're done with it. Close a channel only when it is essential to inform the receiving goroutines that all data has been transmitted.

We can easily close a channel in golang by using the close() function. This built-in function sets a flag indicating that no additional data will be sent to this channel.

Syntax

We can also check whether a channel is open or closed with the help of the given syntax:

If the value of ok is true, this indicates that the channel is open and read operations can be done. Furthermore, read operations won't occur if the value is false, indicating that the channel is closed.

How to close a channel in Golang?

We can easily close a channel in golang by using the close() function.

Syntax

Let's take a simple example in which we create a channel my channel, send data to it, and close it using the close() function.

Program

Explanation

This program simply creates a channel mychannel using the make() function. At last, we close mychannel using the close() function, which sets a flag indicating that no additional data will be sent to this channel.

Output

What Happens if we try to Send Data After Closing a Channel in Golang?

A closed channel still allows us to read data from it but cannot transmit data to it.

Program

Output

The program panic on sending data to the closed channel.

Conclusion

  • A channel in Go is a medium through which a goroutine communicates with another goroutine in a lock-free manner.
  • A channel is created in using the chan keyword or make() function, and it can only transfer data of the same type.
  • The close() function is used to close a channel. This built-in function sets a flag indicating that no additional data will be sent to this channel.
  • A closed channel signifies a circumstance in which we wish to demonstrate that the work on this channel has been completed, and there is no need for it to be open.
  • A closed channel still allows us to read data from it but cannot transmit data to it.
  • We don't have to close every channel when you're done with it. Close a channel only when it is essential to inform the receiving goroutines that all data has been transmitted.