Elegant use of GoLang channels with AWS SQS

Marcio Ghiraldelli
2 min readNov 13, 2018

Some months ago I started using GoLang and was particularly interested on the elegant support it provides for parallelism, in particular the channels type.

As the documentation states:

A channel provides a mechanism for concurrently execution functions to communicate by sending and receiving values of a specified element type.

https://golang.org/ref/spec#Channel_types

I have recently rewritten a NodeJS script in GoLang using channels to consume an AWS SQS queue. See below how elegant this solution was.

The consumer function

First, I created a function which polls the SQS queue in an infinite loop, and publishes the results on a GoLang channel.

Line 1: chn parameter in write mode (<-). That makes the code much more readable, because you quickly understand this function will write data into the channel, not read from it.

Within the infinite loop:

Lines 3–8: AWS SQS SDK method to poll the SQS queue. This method returns an array of available messages to be consumed, limited by const sqsMaxMessages number of messages per iteration.

Lines 14–16: We iterate over the messages returned by the Poll command and send the results over the channel.

The main function

Consuming a channel is just a matter of iterating over it, same way you would do it over an array or slice. By default channels are unbuffered, meaning that the go routine will block after publishing one message before another routine had consumed yet.

Line 3: Declaring a buffered channel. The first argument is the data type, in this case the AWS SQS SDK message type, and the second argument is the channel buffer size, to allow go routines run in parallel and don’t block.

Line 4: Using the go prefix, I’m launching a GoRoutine to execute the poll infinite loop function. The control returns immediately to the line below

Lines 8–11: Iterating over the channel to process the messages

Summary

GoLang GoRoutines and channels provides and simply and elegant way to deal with parallelism and message exchange between concurrent routines.

These characteristics allows you write complex concurrent logic, easy to understand and with little boilerplate code.

If you want to ramp up with GoLang, I suggest Naveen’s Ramanathan excellent tutorial https://golangbot.com/learn-golang-series/

--

--

Marcio Ghiraldelli

Passionated by cloud architecture, process automation and agile environments. Expertise in leading teams aiming high productivity balancing throughput and risk