Golang's 'Defer' Keyword: What It Is and How It Works

In this post, we will learn about the special defer keyword in Go. We will take an in-depth look into deferred statements and learn their uses, as well as some common pitfalls to look out for. If you just want to see the code, you can find the complete examples on Github Basic Usage The defer keyword instructs a function to execute after the surrounding function completes. Let’s look at a basic example:...

Using an SQL Database in Go (With Production Readiness)

This post will go through how to interact with an SQL database in Go, and how to make your Go application production ready when it comes to connecting and querying your database server. If you just want to see the example code, you can view it on Github The “database/sql” Standard Library Let’s go through the architecture of the standard library package used for interacting with SQL databases in Go: database/sql....

Using Enums (and Enum Types) in Golang

This post will describe how to implement and use enumerations (or enum types) in Go. Enums are types that contain only a limited number of fixed values, as opposed to types like int or string which can have a wide range of values. This is useful for many situations: For example, when describing seasons in temperate climates, you’ll want to limit the options to certain possible values: Summer, Winter, Spring and Autumn....

Using a Mutex in Go (Golang) - with Examples

In this post, we’ll discuss why we use mutexes in Go, and learn how to use a mutex to place a lock on data and fix race conditions. Go allows us to run code concurrently using goroutines. However, when concurrent processes access the same piece of data, it can lead to race conditions. Mutexes are data structures provided by the sync package. They can help us place a lock on different sections of data so that only one goroutine can access it at a time....

Implementing a Kafka Producer and Consumer In Golang (With Full Examples) For Production

In this post we will learn how to create a Kafka producer and consumer in Go. We will also look at how to tune some configuration options to make our application production-ready. Kafka is an open-source event streaming platform, used for publishing and processing events at high-throughput. There are a lot of popular libraries for Go in order to interface with Kafka. For this post, we will be using the kafka-go library (but the same concepts will apply for any other library as well)....

Implementing RSA Encryption and Signing in Golang (With Examples)

This post will describe what the RSA algorithm does, and how we can implement it in Go. RSA (Rivest–Shamir–Adleman) encryption is one of the most widely used algorithms for secure data encryption. It is an asymmetric encryption algorithm, which is just another way to say “one-way”. In this case, it’s easy for anyone to encrypt a piece of data, but only possible for someone with the correct “key” to decrypt it....

Type Conversion and Type Assertion in Golang - Everything You Need to Know (With Examples)

When programming in Go, using types is one of the most fundamental language features that we use. Occasionally, we will need to convert between types and interfaces. For example, we may need to convert an int type to a long type. Or maybe we have to convert a string to a []byte type. In this article, we will see how to convert between types in Go, and when to use type assertions and type conversions....

A Guide On SQL Database Transactions In Go

This article explains what SQL database transactions are, and how to implement them in Go (Golang). Transactions are very useful when you want to perform multiple operations on a database, but still treat them as a single unit. We will go into detail on why this is useful, and how you can use it in your Go applications. To illustrate our examples, we will use Postgres as our database of choice....

Making an interactive Telegram bot in Go (Golang)

This tutorial will go through a straightforward set of steps to get a responsive telegram bot up and running from scratch in Golang I spent a considerable amount of time figuring out how to make a functional telegram bot. I mean sure, the official introduction is good, but theres a lot of stuff about what bots are, and a few scattered instructions about the API, but not enough of structure for a beginner to get up and running quickly....

Functional Options in Go: Implementing the Options Pattern in Golang

This post talks about what functional options are in Go, and how we can use the options pattern to implement them. Functional options take the form of extra arguments to a function, that extend or modify its behavior. Here’s an example which uses functional options to create a new House struct: h := NewHouse( WithConcrete(), WithoutFireplace(), ) Here, NewHouse is a constructor function. WithConcrete and WithFireplace are options passed to the constructor to modify it’s return value....