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

 · Soham Kamani

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

 · Soham Kamani

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

Last Updated:   · Soham Kamani

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

 · Soham Kamani

Session Cookie Authentication in Golang (With Complete Examples)

In this post, we will learn how to authenticate users using session cookies in a Golang server application. When a user logs into our application, we need to know who they are across all our HTTP methods and routes. One way to do this is to store the users “session”. A session is started once a user logs in, and expires some time after that. Each logged in user has some reference to the session (called a cookie), which they send with their requests....

Last Updated:   · Soham Kamani

How to Implement Password Authentication and Storage in Go (Golang)

This post will explain how to sign up and login users using password based authentication in Go. Any application that stores passwords has to ensure that its passwords are stored securely. You can’t just store passwords as plain text, and it should ideally be impossible to guess your users passwords, even if you have access to their data. In this post, we will explain the production-ready method for storing passwords, and build a web application in Go to demonstrate it in practice....

Last Updated:   · Soham Kamani

Data races in Go(Golang) and how to fix them

Go is known for how easy it is to build concurrent programs in it. But, with all this concurrency, comes the possibility of the dreaded data race – one of the hardest bugs to debug if you’re ever unfortunate enough to encounter it in your code. In this post, we will go through a sample program that causes a data race, and detect the race condition with the race detector tool....

 · Soham Kamani

A Complete Guide to JSON in Golang (With Examples)

In this post, we will learn how to work with JSON in Go, in the simplest way possible. We will learn how to convert from JSON raw data (strings or bytes) into Go types like structs, arrays, and slices, as well as unstructured data like maps and empty interfaces. JSON is used as the de-facto standard for data serialization in many applications, ranging from REST APIs to configuration management. By the end of this post, you’ll get familiar with how to marshal (encode) and unmarshal (decode) JSON in Go....

Last Updated:   · Soham Kamani

Build a web application in Go (golang)

Go is getting more and more popular as the go-to language to build web applications. This is in no small part due to its speed and application performance, as well as its portability. There are many resources on the internet to teach you how to build end to end web applications in Go, but for the most part they are either scattered in the form of isolated blog posts, or get into too much detail in the form of books....

Last Updated:   · Soham Kamani