4 ms·
I have been using go professionally for a while and I’ve found it to be quite remarkable and I wanted to share a few hints to people so they can find those rema
by evanmoran 4y ago
I have been using go professionally for a while and I’ve found it to be quite remarkable and I wanted to share a few hints to people so they can find those remarkable parts faster than I did.
To get a better sense of go there are five essential concurrency features:
1. Go statements are a nice syntax to run background micro threads (as mentioned in this article)
2. Go Channels pass messages across those threads as fixed memory queues and powerfully as you add items to the queue you block until the item is added (the second part, blocking on add, is very powerful as it prevents the flow of concurrency from infinitely filling queues that are never consumed! There is more to go channels that is possible with fixed memory buffering to prevent the block, but that blocking is key to consider!)
3. Go Select Statements (not switch statements!) let you watch concurrent queues at once in a thread safe way. These are essential for using channels properly and they help manage almost all channel flow (consumer thread progression, error handling, done detection, etc)
4. Go Context objects let you cleanup background threads based on multiple criteria server errors and timeouts being the most common. The best hint you are in a concurrent-friendly function is usually that it passes context as an argument for cleanup purposes.
5. Go wait groups let you wait for all the go statements spawned to finish before proceeding (simple, but essential at times)
I know it’s hard to learn the entirety of a langue without using it daily, but I encourage people to try out go to experience these five parts together. Go is truly excellent at expressing some hard concepts well. That doesn’t make it easy — concurrency isn’t easy — but it is easier with go constructs than without.
- t_mann 4y ago> Go statements are a nice syntax to run background micro threads (as mentioned in this article) That's literally the opposite of what the article says. Which btw isn't about Go specifically, but about concurrency in general.
- freiherr 4y agoWell go statements are as nice as you can get with old-school concurrency primitives. Author of the article believes we need to have new completely concurrency primitives.
- t_mann 4y agoYes, you may agree or disagree with that, and please do share your thoughts, but the discussion should stay on topic. The parent comment goes on a lengthy tangent why they like Golang that is completely unrelated to the article.
- evanmoran 4y agoApologies if it didn’t come across, but the point I was trying to make is that these five features together are the elegant part of go concurrency. So the comparison of go statements alone to other concurrency approaches is missing the best parts of the language (contexts and channels) For example, if you use go statements without go context you can’t clean up background threads with error handling, and if you don’t use channels + select statements together then you can’t synchronize across threads easily. These features have to be used together and are in my experience (C++/game development) much better than most other async approaches. All that said, these features are quite low level and not obvious when first learning (hence my explanation above) so higher level libraries definitely need to exist (and do exist). It’s just these libraries are much easier to write in go because these primitives are quite powerful.