Using Iota in Golang - Declaring Incremental Constant Values with Iota

In this post, we will learn about the “iota” identifier in Go, how it’s used, and when not to use it. The “iota” identifier is used to represent integer based constants in Go, and is a convenient way to declare a sequence of constants, while keeping the code readable. Using Iota for Constant Declaration Normally, constants can be declared by specifying its value, for example: const ( SPADES = 1 HEARTS = 2 DIAMONDS = 3 CLUBS = 4 ) func main() { fmt....

 · Soham Kamani

Golang Constructors - Design Patterns For Initializing Variables

A constructor is a language feature that is used to initialize variables. The Go language doesn’t have any concrete “constructor” as such, but we can use a number of different language features to idiomatically initialize variables. In this post, we will go through the different types of constructors that we can use in Go, and in what situations you should use them. You can view the code to run all examples mentioned here on the Go playground...

 · Soham Kamani

How to Lazy Load Disqus Comments in Your Hugo Webpage

In this post, we will illustrate how to lazy load your Disqus comments on your Hugo site, so that they show up only once the user scrolls down to the comment section. This can potentially save a lot of bandwidth for someone viewing your page, since the assets downloaded by Disqus can be quite heavy. Create Your Own Comments Partial By default, Hugo uses an internal partial template to render Disqus comments:...

 · Soham Kamani

Implementing Set Data Structures in Golang (With Examples)

As you may know, there is no native implementation of sets in Go. This post describes how we can use the existing native data structures to implement sets (and hash-sets) in Go. What Is a Set? A set is a data structure that holds a distinct collection of items. It’s useful when you want to hold a non-repeating list of objects. A good example of a set is describing the types of animals that are present in a zoo:...

 · Soham Kamani

Time in Golang - Working With Time, Duration, and Dates (With Examples)

This tutorial will teach you how to work with Time in Go (Golang) - including how to parse and format date-time strings, add and subtract time durations, and get the current system time. The main library that we’ll use when working with times and dates is the time standard library. Getting the Current System Time Let’s begin with an example of creating a new Time instance from the current system time:...

 · Soham Kamani

Golang's "defer" Explained (With Examples)

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:...

 · Soham Kamani

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....

 · Soham Kamani

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....

Last Updated:   · Soham Kamani

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....

Last Updated:   · Soham Kamani

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....

 · Soham Kamani