Channel release in Go

I have a TCP socket and 2 goroutines that read and write to streams. I am writing a goroutine that reads data from a pipe. If the TCP connection is dropped, then reading from the subroutine will catch an error and stop.

But how can I free the channel that goroutine is written on?

Is there a method, for example chan.release()

, or should I send a special packet that tells the goroutine to end?

+3


source to share


1 answer


close(ch)

will close the channel.



If you are on a channel, then the for loop will close when the channel is closed. val, ok := <-ch

allows you to check for closed channels. ok will be a boolean false if ch is closed true if ch is open.

+5


source







All Articles