Friday, May 17, 2024
HomeGolangWhat occurs when a channel goes out of scope - Getting Assist

What occurs when a channel goes out of scope – Getting Assist


What occurs when a channel goes out of scope

Hello;

right here is an instance.

predominant() calls func1()
func1() declares a channel, begins a goroutine, and terminates.
goroutine writes to the channel (with out blocking because the channel is buffered)
and terminates too.

func predominant() {
	func1()
	time.Sleep(1 * time.Second)
}

func func1() {
	ch := make(chan int, 1)
	go func() {
		ch <- 1
	}()
}

The channel goes out of scope and there are not any references to it anymore. Will it
be rubbish collected? Is there the rest to the channel that must be
achieved? Is that this code right?

Thanks,
igor

Channels are rubbish collected similar to every thing else. There are many references on the internet that state that closing a channel shouldn’t be essential for it to be collected.

Thanks Jeff.

It seems that my confusion was as a result of the truth that I’m leaving the channel
with a knowledge merchandise in it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments