Go: proposal: Go 2: run `for` loop in a goroutine with `go` keyword

Created on 6 Jan 2020  路  4Comments  路  Source: golang/go

Go 2 Proposal

go keyword runs a function in a goroutine. So when you want to run a for loop in a separate goroutine, you have to do such thing:

go func() {
    for {
        // do something regularly
    }
}()

What if you could run a for loop like this ?

go for {
    // do something regularly
}

I know it's a little confusing when it comes to considering the scope but I think it looks clean and minimal.

Go2 LanguageChange Proposal Proposal-FinalCommentPeriod

Most helpful comment

Why should we permit specifically go for and not any other kind of statement?

The current go statement simply accepts any function call. That is easy to understand. Supporting go with a for statement doesn't seem to me like a particularly common case, and I don't see why we should add a special case for it.

All 4 comments

This looks like related to #24210.

Why should we permit specifically go for and not any other kind of statement?

The current go statement simply accepts any function call. That is easy to understand. Supporting go with a for statement doesn't seem to me like a particularly common case, and I don't see why we should add a special case for it.

Based on emoji voting this proposal is not popular. Also as noted above it is not general. For these reasons, this is a likely decline. Leaving open for four weeks for final comments.

I know it's a little confusing when it comes to considering the scope but I think it looks clean and minimal.

The problem with both this proposal and the normal way of writing it (go func() { for { ...) is that the stacktraces are ugly when you're debugging a server. It's nicer to give those permanent goroutines names.

Was this page helpful?
0 / 5 - 0 ratings