Closing the Channel in Golang

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.