Using Maps in Golang - With Examples

Maps are a built-in data structure in Go that act as a collection of key-value pairs. They are also known as associative arrays, hash tables, or dictionaries in other programming languages. In this post, we will learn about maps in Golang, and how to use them. We will learn about the syntax, how to create a map, how to add and delete elements from a map, how to iterate over a map, and how to check if a key exists in a map....

Making REST API Requests in Golang using the HTTP Client

In most web applications, we need to make HTTP requests to external services. For example, we might need to make a request to a third-party API to fetch some data. Or we might need to make a request to our own API to create, update, or delete some data. In this post, we will learn how to make REST API requests (like GET, POST, etc) in Golang using the HTTP client....

How to Use Iota in Golang

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. If you just want to see the code, you can view it on Github or run it on the Go playground...

Golang - Implementing Heap Data Structure (and Heap Sort)

In this post we will learn about heap data structures, what they are used for, and how to implement them in Go using the container/heap standard library If you just want to see the example code, you can view it on Github or run the live example on the Go playground What Is a Heap Data Structure? A heap is a tree-based data structure with a fixed relationship between each parent node and its child nodes....

Variables in Golang - Everything You Need to Know

This tutorial will explain how to work with variables in Golang, and some conventions used for idiomatic Go code. Variables in Go, just like in other languages, are used to store and represent data. Every variable in Go has a type and a value. If you just want to read some example code, you can view it on Github. Declaration Lets look at the declaration of an integer type: var i int = 5 This declares a variable named i of type int with a value of 5....

Executing Shell Commands in Golang

In this tutorial we will learn how to execute shell commands (like ls, mkdir or grep) in Golang. We will also learn how to pass I/O to a running command through stdin and stdout, as well as manage long running commands. If you just want to see the code, you can view it on Github The Exec Package We can use the official os/exec package to run external commands. When we execute shell commands, we are running code outside of our Go application....

Golang Make Function - Initializing Slices, Maps, and Channels (Size and Capacity)

Go has a special make function that can be used to initialize channels, slices, and maps. Using make, we can specify the memory and capacity constraints of the data type being created, giving us low-level control that’s not available to us using regular constructor functions Basic Usage make is a special function in Go that can take a different number of types and arguments. It returns an instance of the type passed as the first argument:...

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

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

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