4 ms·
Everything can be a coroutine (goroutine) in Go, very simple. Just add `go` before the function call. No need to do `async` and `await` or try to make sure it's
by gunapologist99 3y ago
Everything can be a coroutine (goroutine) in Go, very simple. Just add `go` before the function call. No need to do `async` and `await` or try to make sure it's turtles all the way down by never calling a synchronous function from within a `async` function. In Go, everything within the function proceeds synchronously unless you use `go`.
It's literally as simple as it sounds:
https://gobyexample.com/goroutines https://gobyexample.com/goroutines
Want to wait for a bunch of parallel coroutines to finish (sync) before proceeding? Sure, that's easy too:
https://gobyexample.com/waitgroups https://gobyexample.com/waitgroups
This simple primitive is one of two major go features that made me not really pursue Rust more, simply because Rust's concurrency libraries (it doesn't seem to be an intrinsic part of the language like Go) seem to be more modeled after Python's or Node's. Go's `go` is even more readable than Erlang and Scala, so in my opinion it's one of the great three features of the language
(OT, but to me the other two major features are 1: channels being an integral part of the language, and 2: forced immediate error handling -- perhaps you disagree that this second one is a feature and prefer tracebacks, but in my experience large Go projects always seem to have cleaner code than, say, Python, probably because errors are handled very close to their origination. Lastly, there are a minimum of footguns in Go.)
- HumblyTossed 3y ago> No need to do `async` and `await` or try to make sure it's turtles all the way down by never calling a synchronous function from within a `async` function. Yes, good grief this is annoying.
- randomdata 3y agogoroutines do not give the programmer control over suspension points, though, so they differ from coroutines as most people are apt to think of them. There was consideration for adding coroutine[1] support for the rangefunc experiment, but it didn't make the cut for 1.22, opting for a simpler model. [1] https://research.swtch.com/coro https://research.swtch.com/coro
- gunapologist99 3y agoThat's what the other great feature of Go, channels, are for. https://softchris.github.io/golang-book/05-misc/04-goroutines/ https://softchris.github.io/golang-book/05-misc/04-goroutine... Channels let you block (control suspension points) and wait for progress from any other thread.
- flooow 3y ago'goroutines' are just threads, that happen to be scheduled in userspace. They are not coroutines - they don't work the same way and don't have the same features. Pretty much nothing in the blog post is possible is Go, without 'writing it yourself', which is what you are doing with channels. The same is possible in most other languages.
- jerf 3y agoThat's not really a coroutine. It is a set of language features that can be used to solve similar problems in a different way, and while conflating those two things is a popular programmer past time, they aren't the same thing. Coroutines are one of those terms that used to have a really price academic definition and has gotten a lot looser over the years. Python generators don't conform to the academic definition, but casually calling them coroutines is pretty popular now. However, no matter how you slice it, goroutines aren't coroutines and that's not the derivation of the name. They're threads. Within those threads is bog-standard structured programming. All functions have one entry point. Being able to push out values through channels is no different than a normal function that writes several things to a file, it doesn't have the distinctive characteristics of a coroutine.
- agent281 3y ago> Coroutines are one of those terms that used to have a really price academic definition and has gotten a lot looser over the years. Python generators don't conform to the academic definition, but casually calling them coroutines is pretty popular now. Could you elaborate on how Python's implementation doesn't meet the precise definition?
- 3y ago
- atomicnumber3 3y agoI've been in a go job recently, and I have to say, I hate the entire language - a lot - except goroutines. I normally complain a lot (in my head to myself, not to anybody else) about literally everything not being written in Java, but with for this service go was actually a really solid choice at the time. The green thread approach + writing "blocking" code is far superior to async/await or event-based code and the product wouldn't been a mess if it were using a less straightforward programming model. But now that Java has the same feature in the form of virtual threads, I'm once again back to complaining!
- HumblyTossed 3y agoOk. But, surely there are places you could go and use Java.
- deleted 3y ago[deleted]
- atomicnumber3 3y agoLanguage choice is low on my list of reasons I choose a company, but there's no law that says I can't still complain to myself ;)
- gpderetta 3y agoThe irony of a language with first-class one shot continuations not having exceptions is not lost.
- randomdata 3y agoWhich language is that? Context would suggest you are referring to Go, but Go has exceptions. It even has exception handlers. As do all of the other languages mentioned.