Learn to code by coding - try our free CS courses

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 function 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 every developer’s nightmare. While no language or package manages time perfectly, I think Golang does a pretty good job out-of-the-box. This full tutorial should answer ~90% of the questions you’ll have about time management in Go. Table of Contents Overview – How dates and times are …

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

As a language designed for the web, Go provides extensive support for working with JSON data. JSON (JavaScript Object Notation) is an incredibly popular data exchange format whose syntax resembles simple JavaScript objects. It’s one of the most common ways for applications to communicate on the modern web. Table of Contents Encoding and decoding with …

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 and yet simple and unique values. Example of an idiomatic enum Within a constant declaration, the iota keyword creates enums as successive untyped integer constants. Why should …

Read more

Splitting a String into a Slice in Golang

splitting a lemon

I can’t begin to tell you how often I split strings in Go. More often than not I’m just parsing a comma-separated list from an environment variable, and Go’s standard library gives us some great tools for that kind of manipulation. Table of Contents Split by commas or other delimiters strings.Split strings.SplitN Split by delimiters …

Read more

All the Ways to Write for Loops in Go

loop architecture

A for loop executes a block of code repeatedly, and in Golang, there are several different ways to write one. The standard three-component loop For-range loop Range over slice Range over map Range over channel Range over string While loop Optional components loop Infinite loop Break from a loop Continue (skip to the next iteration) …

Read more

How I Organize My Local Development Environment

organize

When I was just getting into coding, I was very disorganized. I would create a new text file in My Documents, work on it, never create a Git repository, accidentally delete it later, you get the idea. Nowadays I’m quite the opposite. To be honest, the thing that made me get my act together was …

Read more