Async/Await in Golang: A Beginners Guide
Async/Await is a feature of a language that helps simply an interface and achieve asynchronous programming. Golang serves as a concurrent language for programming that contains some powerful features such as Channels and Goroutines. These can be implemented with an interface and a simple structure in the Golang language using the keywords “async/await” along with “goroutines”. The implementation generally begins with the start of the package directory in the main.
As a programming language, Golang contains some powerful features such as Channels and Goroutines that help in the efficient asynchronization of various tasks. Goroutines serve as not OS threads. This means that they are able to spin as many goroutines as is necessary for the developer without adding a large overhead. The size of the stack at the beginning is set at 2KB. Most developers prefer the async/await feature as it helps provide a simpler interface and supports asynchronous programming.
How Does it Work?
Async/await was first introduced in F# and C#. As it gained, it was also incorporated into Javascript and Python. It is able to simplify the asynchronous execution method and the structure. It achieves this while reading like synchronous code. And this makes life easier for the developers to follow. Given below is an example of async/await working in C#.
The main function in the program is executed as soon as the program runs. The DoneAsync here works as the async function. The execution of the code comes to a halt at the Delay function. It is programmed to introduce a delay of 3 seconds. The Delay function is an async function so it is called using await. Await works by blocking the execution of the code within the async function. The DoneAsync function in the main is not called with the await, however, the execution begins for DoneAsync. This is the only way to get a result, with the executed code having the following flow:
The execution is asynchronous but the executed code looks simple. Now we see how to do this using Golang utilizing Channels and Goroutines.
In the code above, the DoneAsync is running asynchronously and returning a channel. It is writing a value into the channel after the execution of the async task. The DoneAsync is invoked from within the main function, it does its operation and reads the value from the returned channel. It acts as a call that blocks, waiting till the return of a value and getting that value written into the channel, and then writing it onto the console.
The code has the same output as the C# program. But the look is not as elegant as that of when async/await was used. This may be good, but developers can do a lot of granular things with the help of this approach more easily. Developers are also able to use and implement the async/await keywords in the Golan language with the help of an interface and a simple structure.
Implementing Async/Await
It is important to start working with the package directory that is named async for implementing async/await in the Golang programming language. The structure of the project is shown below:
The async file is used for storing the simple future interface that is designed to have the ability to handle simple async tasks.
The await method signature is present in the future interface that has been added here. Then a future structure is holding a single value, which is the fiction signature of the await function. The future structure is implementing the await method of the future interface. This is done through invoking its own await function.
The exec function is being used for the execution of the passed function in an asynchronous manner in a goroutine. Then the await function is returned. This function is waiting for the channel to either close or the context to get read from. On the basis of which event occurs first, the function will return an error or give the result, the result being an interface. With the help of the new async package, we now attempt to change the go code.
After one look, the code presents a cleaner image. The code is not specifically working with channels or goroutines. The DoneAsync function has been modified so that its nature is now completely synchronous. The main function makes use of the exec method of the async package for the handling of the DoneAsync. This makes the DoneAsync start its execution. The flow of control of the program returns to the main function which is capable of executing other pieces of code. In the end, a blocking type call is made to await and the data is read back.
The code now presents a cleaner picture, making it easier to read, comprehend and simpler to understand. The async packages can be modified by the developer in order to incorporate numerous other types of asynchronous tasks with the help of Golang. This guide only covers the simple implementation.
This guide covers what async/await is and how it can be implemented using the Golang programming language. The readers are encouraged to research more into async/await and implement it to understand how it provides readability to the codebase.