News for hackers. Stuff that matters.
Re:Golang: Know Your 'Nil' by ben509 on Tuesday March 30, 2021 @01:25PM (#26637501)
> Writing to and reading from a nil channel blocks forever.

Have any experienced Go devs found cases where this behavior is useful?

Re:Golang: Know Your 'Nil' by jrockway on Tuesday March 30, 2021 @02:14PM (#26638179)
Personally, I never read or write a channel without some sort of time bound, so this behavior doesn't really bother me:

    select {
    case foo := <-whateverCh:
        log.Printf("hey a foo: %v", foo)
    case <-ctx.Done():
        log.Println("the user doesn't care about the answer anymore, so let's not leak the goroutine")
    }
Re:Golang: Know Your 'Nil' by SmooL on Tuesday March 30, 2021 @01:51PM (#26637875)
Actually yes - it implies that if the read/write executes, then the read/write was successful.

In practice, in cases where a nil read/write could happen, a default "fall through" option on a select statement is used.

They could have made the original interface return a possible error for reading/writing, in line with regular go error handling, but opted instead to use more channel-based conventions.

Re:Golang: Know Your 'Nil' by tedunangst on Tuesday March 30, 2021 @01:55PM (#26637926)
I think the question is when would it be useful outside of select. Once you block on nil channel, there's no way to unblock, which doesn't seem helpful.
Re:Golang: Know Your 'Nil' by jrockway on Tuesday March 30, 2021 @02:19PM (#26638227)
Such is the danger of blocking indefinitely. I think every Go programmer's first concurrent app eventually crashes because it runs out of memory, memory used by goroutines that are waiting for an answer that will never come. When you write "foo := <-ch", you're saying "I am willing to wait forever for an answer". Unfortunately, actual computers in the real world don't have the resources to wait forever.

Honestly, I see the ability to block indefinitely as a bug in the language. An improved language would probably "result, err := <(ctx)- ch", i.e. make it impossible to block without something bounding the duration of the block. (Also kill sync.Mutex. 100% of Go concurrency bugs are from people mixing mutexes and channels.)

(As an alternative, the runtime could be smarter about goroutines that have blocked forever. I am not sure exactly what it would look like, but the hypothetical first Go program I talk about above would probably be saved by something that killed goroutines that were waiting for a message from a TCP connection that is long gone. I think if it were easy to do right, it would have been done, though.)

Re:Golang: Know Your 'Nil' by konart on Tuesday March 30, 2021 @02:18PM (#26638214)
They only case I can think of right away is some kind of process that should not die after the execution and wait until the user kills it by hand after reading through execution report.
Re:Golang: Know Your 'Nil' by toqueteos on Tuesday March 30, 2021 @04:40PM (#26640154)
You can disable/pause channels using this. "Advanced Go Concurrency Patterns" by Sameer Ajmani explained this back on 2013.
Re:Golang: Know Your 'Nil' by sagichmal on Tuesday March 30, 2021 @08:27PM (#26642183)
Yep, it's very useful for channel-based state machines, you can turn transitions on and off between states.