Describe how to close a channel and why it's important.

  • Use the close() function; it signals no more data.
  • Set the channel to nil to close it.
  • Channels are automatically closed when unused.
  • Closing a channel is not possible in Go.
In Go, you close a channel using the close() function. It's important to close a channel when you're done sending data to it to signal that no more data will be sent. This is crucial for Goroutines waiting on the channel to know that they should stop waiting and exit. Failure to close a channel can lead to deadlocks or Goroutines waiting indefinitely.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *