Creating workflows in Go

Kartik Sirigeri
2 min readDec 2, 2020

Executors and Futures in Go

Parallelism is about doing many things at once. While this sounds similar to concurrency but they are different. Go provides Goroutine to achieve parallelism. If you are familiar with Java, Goroutine can be thought as threads in Go for the sake of understanding, while actually they are different.
Many Goroutines are actually multiplexed onto fewer OS threads.

This article is not about what Goroutines are and its workings. If you are not familiar with Goroutine yet, you should familiarize before proceeding further.

Though Goroutines are very straightforward to use by any developer, it could be prone to error for a naive programmer, especially when there is a need to get some data back from the goroutine or there is a need to coordinate among the Gorotuines.

Below is a sample code that triggers a method as a Goroutine and waiting for the completion of the Goroutine,

Same can also be achieved by making use of Channels in Go.
Now, imagine if you need to create many such Goroutines and you need some kind of coordination among them. This is easily prone to errors/race conditions if not careful.
Let’s say you need to create a simple flow,
1. Generate a bill for the items in the cart (by making an HTTP call to some billing service)
2. Get the current dollar conversion rates (again by making an HTTP call to some exchange service)
3. Generate the bill amount in $ by combining the outputs of above 2 steps

The above steps can be easily achieved by writing Goroutines and coordinating among them using channels/waitgroup (or simply writing a syncrhonous code but we want to be fast).
The above scenario can be achieved using the GO-WORK-FLOW library. Below is a code snippet written for this scenario using GO-WORK-FLOW library,

getBillAmount, getDollarValue and generateBillAmountInDollar are methods that return int value. generateBillAmountInDollar takes two int parameters. All of these functions are executed separately by Goroutines.

Awesome! We generated the bill in $ by writing our code in a more intuitive way. The above code snippet is more readable and easily maintainable.

If you have come till here you should start exploring the utility GO-WORK-FLOW.

--

--