Thursday, January 14, 2021

Go (golang) Goroutines - Running Functions Asynchronously

I've been exploring the Go programming language (golang). A really cool feature is that running functions asynchronously is built right in to the language.

How to Run a Function Asynchronously

Normal function call:


Asynchronous function call:

    go processData(incomingData)

Add a "go" before the call. Now it is a goroutine, and it runs asynchronously.

That's it. That's the post.

So, there is more...
Okay, asynchronous programming is more complicated than this. Once we have multiple things running at the same time, we need to know when they complete and often communicate results between asynchronous processes. In Go, we can use "WaitGroup" and "channel" to help with this. But that's a bigger topic for another article.

Additional Articles:

Motivation: I have been using C# as a primary language for 15 years. Exploring other languages gives us insight into different ways of handling programming tasks. Even though Go (golang) is not my production language, I've found several features and conventions to be interesting. By looking at different paradigms, we can grow as programmers and become more effective in our primary language.

Resources: For links to a video walkthrough, CodeTour, and additional articles, see A Tour of Go for the C# Developer.

Happy Coding!

No comments:

Post a Comment