|
|
|
|
|
by amiga386
17 days ago
|
|
I think you're missing something. Go's functions are all blue, not all red. As far as I know, the callers of async functions with Rust's Tokio and C#'s Tasks themselves need to be async; not the case for Go. Concurrency is usually a mix of goroutines and channels. There is no inherent link between caller and asynchronous callee. You can use goroutines without channels, and channels without goroutines. You can write "go" to launch any function call in its own goroutine, but you cannot get a return value from it. This isn't valid: user_is_valid := go validate_user(u)
The idiomatic way you can do that is to use a goroutine and a channel: ch := make(chan bool)
go func() { // runs asynchronously
ch <- validate_user(u) // blocks until it can send
}()
user_is_valid := <-ch // blocks until it can receive
return user_is_valid // ta-da, blue function returning red function result
I don't think it's a boon to have functions "coloured for you" to tell you that they might block. On the other hand, functions that would block tend to accept a Context parameter to let you control what they should do. It's a major indicator that the function's probably going to do something async, but it doesn't have to. |
|
My point about the colouring is that it's actually nice to have explicit colouring, in go the asynchronicity of functions aren't encoded by the type system but in practice you still handle them differently. You can't just call one of them without passing in things like context, or handling channels without refactoring anyways.