News for hackers. Stuff that matters.
Re:Go Things I Love: Channels and Goroutines by SamReidHughes on Tuesday January 07, 2020 @12:54AM (#21977314)
Go channels have a fixed capacity, by default zero, and their main special feature (compared to just having some concurrent_queue<T> type) is the select statement, which helps devs do things the right way. Nothing stops you from stupidly locking the system, e.g. by having a thread block itself by writing to the channel it’s responsible for consuming.
Re:Go Things I Love: Channels and Goroutines by jayd16 on Tuesday January 07, 2020 @01:20AM (#21977445)
Is the select semantically different than something like this?

    bool TryWrite(T)
How does go handle cancellation with the select syntax? I guess you have a cancel channel and select across both the cancel and the read channels? Is Go smart enough to sleep and not busy wait in that case?
Re:Go Things I Love: Channels and Goroutines by TheDong on Tuesday January 07, 2020 @01:34AM (#21977529)
> Is the select semantically different than something like this?

Yes. select can do a lot of things. It's overloaded to be something like 5 different things. Here, let me show you:

    chanOne := make(chan int)
    chanTwo := make(chan int)
    ctx := context.Background()

    // Receive whichever is ready first. Blocking
    select {
    case v := <-chanOne:
    case v := <-chanTwo:
    }

    // Non-blocking read
    select {
    case v := <-chanOne:
    default:
    }

    // Timeout and cancellation
    select {
    case v := <-chanOne:
    case <-time.After(1 * time.Second):
    // timeout
    case <-ctx.Done():
    // cancel
    }

    // Write or read, whichever channel is ready first
    select {
    case chanOne <- 1:
    case v := <-chanTwo:
    }

Another form of cancellation is closing channels, which causes reads to return the zero value immediately, and causes any writes to that closed channel to immediately panic (effectively abort the program).
Re:Go Things I Love: Channels and Goroutines by jayd16 on Tuesday January 07, 2020 @12:00PM (#21981819)
Neat. Most of the permutations are easily handled in other languages as well except for the full blocking (sleeping) case. Without a channel the scheduler can reason about, most languages would have to busy wait or possibly pull from multiple channels.
Re:Go Things I Love: Channels and Goroutines by SamReidHughes on Tuesday January 07, 2020 @02:20PM (#21983588)
I think the biggest advantage is that it's a lot harder to screw up using a select statement than most equivalent API's you'd create in other languages that allow waiting on one of N channels/signals/channel/event operations, and branching based off that.

Simply because you're either constructing a bunch of callback objects or you get some number N saying the Nth parameter had an event, and your code has to match N to the specific channel correctly.

Re:Go Things I Love: Channels and Goroutines by Vendan on Tuesday January 07, 2020 @01:40AM (#21977562)
Go does put that goroutine to sleep, freeing the underlying OS thread to work on other goroutines, so it's very lightweight.