Command Pattern in Go (Golang)

This article will explain the command pattern, where to use it, and how to implement it in Go. The command pattern, as the name suggests, is used when we want to create and execute “commands”. Different commands have their own implementation, but have the same steps for execution. The Command Interface The basic unit for implementing the command pattern is the Command interface: type Command interface { execute() } If the command can error out, the interface can contain an error return value as well:...

A Comprehensive Guide of Arrays and Slices in Golang (and their differences)

At first, it’s easy to see arrays and slices as the same thing: a data structure to represent collections. However, they are actually quite different from one another. In this post we will explore their differences and implementation in Go. We will then look at some examples so that you can make a more informed decision on when to use them. Arrays An array is a fixed collection of data. The emphasis here is on fixed, because once you set the length of an array, it cannot be changed....

Golang functions vs methods 👯‍♀️

This post will describe the key differences between functions and methods in Go, and when it’s best to use them. Functions and methods are both used extensively in Go to provide abstractions and make our programs easier to read and reason about. On the surface, functions and methods both look similar, but there are some important semantic differences which can make a big difference to the readability of your code....

Polymorphism in Go - without interfaces 🦆

Ok, so you want to use polymorphism in Go, but don’t like interfaces? Read on… First, let’s see what we want to do: var dog, duck *Animal dog = NewDog("fido") duck = NewDuck("donald") fmt.Println(dog.makeNoise()) // fido says woof! fmt.Println(duck.makeNoise()) // donald says quack! Here, dog and duck are of the same type (*Animal). Each variable is instantiated with a different constructor, and they have different behaviours when the same method makeNoise is called....

Implementing JWT based authentication in Golang

In this post, we will learn how JWT(JSON Web Token) based authentication works, and how to build a server application in Go to implement it using the golang-jwt/jwt library. If you already know how JWT works, and just want to see the implementation, you can skip ahead, or see the source code on Github The JSON web token (JWT) allows you to authenticate your users in a stateless manner, without actually storing any information about them on the system itself (as opposed to session based authentication)....

Last Updated:   · Written by Soham Kamani

Golang Omitempty Explained

If you’ve worked with Go for sometime, you have probably encountered the omitempty tag, when trying to encode a struct to JSON. This post will explain what exactly the omitempty tag means, and some common pitfalls, along with ways to get around them. Basic Usage When using Go to build web applications, or HTTP servers, there will eventually come a time when we need to encode some data as JSON. The problem here is that the JSON representation does not exactly match the way Go handles data....

Creating an OAuth2 Client in Golang (With Full Examples)

In this post we will see how we can implement OAuth2 authentication in a Go web application. We will create a working website that can allow a user to sign in using Github authentication. If you just want to see the code, you can view the full example on Github How OAuth2 Works Let’s take a brief look at the OAuth protocol before we jump into implementation. If you’ve ever seen a dialog like this, then you’ve probably used OAuth before:...

Last Updated:   · Written by Soham Kamani

Factory patterns in Go (Golang) 🏭

The factory pattern is a commonly used pattern in object oriented programming. In Go, there are many different ways in which you can use the factory pattern to make your code cleaner and more concise. Go’s answer to classes, are structs. For example, the struct for a “Person”, along with a “Greet” method would look like this: type Person struct { Name string Age int } func (p Person) Greet() { fmt....

How to Use Context in Golang (Deadlines, Cancellation, and Passing Values)

In this post, we’ll learn about Go’s context package, and more specifically, how we can use it to improve our applications performance. The main purpose of using Context is to manage long-running processes (like an HTTP request, a database call, or a network call) in a way that doesn’t waste resources. If used correctly, the context package can help you: Cancel long running processes that are no longer needed Pass around request-scoped data and cancellation signals between function calls Set deadlines for processes to complete When Do We Use Context?...

How to generate an error stack trace in Go 🥞

A common problem that many people (including me) have faced when programming in Go, is to pin point the source of an error. Other programming languages provide you with a stack trace to tell you where the error came from, but Go does not have this behavior by default. In this article, we will discuss how to use the fmt, and the github.com/pkg/errors libraries to give us better error reporting....