|
|
|
|
|
by slopinthebag
18 days ago
|
|
Say you have a function like validate_user(). In Go, should you block the main thread on this call, or fork and join? What if it makes a DB request and now your UI is blocked for 2 seconds or something? You need to know implicitly you need to call validate_user() in a goroutine, and then deal with forking and joining manually. If it's explicitly coloured as async, you know. In most async/await languages you can run async functions as sync, eg. Tokio's block_on method or C#'s .Result. |
|
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:
The idiomatic way you can do that is to use a goroutine and a channel: 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.