How to Write Insertion Sort in Go

insert

Insertion sort builds a final sorted list one item at a time. It’s much less efficient on large lists than more advanced algorithms like quicksort or merge sort. Insertion sort is a simple algorithm that works just like you would arrange playing cards in your hands. A slice is first split into sorted and unsorted …

Read more

Merge Sort in Golang with Examples

swirl

Merge sort is a recursive sorting algorithm and, luckily for us, it’s quite a bit faster than bubble sort. Merge sort is a divide and conquer algorithm. Divide Divide the input slice into two (equal) halves Recursively sort the two halves Conquer Merge the two halves to form a sorted array Full example of the merge sort algorithm Merge sort actually has two functions involved, the recursive mergeSort function, and the merge function. Let’s write the mergeSort() function first. It’s a recursive function, which means it calls itself, and in this case, it …

Read more

Writing Bubble Sort in Go from Scratch

bubbles in water

Bubble sort is named for the way elements “bubble up” to the top of the list. Bubble sort repeatedly steps through a slice and compares adjacent elements, swapping them if they are out of order. It continues to loop over the slice until the whole list is completely sorted. Full example of the bubble sort algorithm Using the algorithm in code Why use bubble sort? Bubble sort is famous for …

Read more

How to Properly Use Defer in Golang

hand signal

What is the “defer” keyword in Go? In the Go programming language, defer is a keyword that allows developers to delay the execution of a function until the current functions returns. What throws some people off is that the deferred function’s arguments are evaluated immediately, but the function itself doesn’t fire until the wrapping function …

Read more

Comprehensive Guide to Dates and Times in Go

clock

Keeping track of time in code has long been a developer’s nightmare. While no language or package manages time perfectly, I’m of the opinion that Golang does a pretty good job out-of-the-box. This full tutorial is designed to answer ~90% of the questions you’ll have about managing time in Go. Table of Contents Overview – …

Read more

Concatenating with strings.Builder Quickly in Golang

knot in string unsplash

The Go standard library makes concatenating strings easy. Concatenation is just a fancy word for adding strings together to make a larger string. For example, if we concatenate “hello”, ” ” and “world” we’d get “hello world”. The built-in fmt.Sprintf function takes a format and a variadic list of interfaces as input. The formatting option …

Read more

Golang vs Python: 6 Questions to Decide Which Programming Language is Best For You

golang vs python: this image shows a white man with a Gopher image superimposed on his face squaring up to box woth a Black man with a Python logo superimposed over his face.

These two coding languages duke it out – but who’s the winner? Question Tentative Winner Brief historic description of Golang vs Python – Golang vs Python: Which coding language is best for beginners? Python Golang vs Python: Which coding language is best for experienced coders? Go Golang vs Python: Which language is best for a …

Read more

The Ultimate Guide to JSON in Go

json data

Being a language built for the web, Go offers feature-rich support for working with JSON data. JSON (JavaScript Object Notation) is an unbelievably popular data interchange format whose syntax resembles simple JavaScript objects. It’s one of the most common ways for web applications to communicate. Table of Contents Encoding and decoding with struct tags Marshaling …

Read more

Search and Replace Strings in Golang – Top 5 Examples

replace lightbulb

Go has a powerful standard library that makes string manipulation easy right out of the box. One of the functions I use most often is the strings package’s Replace() function. strings.Replace() returns a copy of its input string after replacing all instances of the given substring with a new one. strings.Replace() signature Notes s is …

Read more

How and Why to Write Enums in Go

list with pencil and paper

An enum (short for enumerator), is a set of named constant values. An enum is a powerful tool that allows developers to create complex sets of constants that have useful names yet simple and unique values. Syntax Example Within a constant declaration, the iota keyword creates enums as successive untyped integer constants. Why should you use enums? Let’s look at …

Read more