This is a self reference document, as I’m trying to learn Go and use it as my go-to language (no pun intended).

Fundamentals

Compile

The code you write in Go is translated (compiled) into a single executable file that can be run on any machine without needing to install Go or any dependencies.

This means, for example, that if you want to containerize your application, you can just copy the binary into the container and run it without worrying about installing Go’s compiler or any libraries, which makes it very convenient for distribution and deployment.

I’m personally a big fan of compiled languages, the idea of having a single binary that you can run anywhere is just pleasant to me.

Variables

In Go, variables are declared using the var keyword, followed by the variable name and type, e.g., var userName string.

As always, there are some types that can be inferred: int, float64, string, bool, and byte (literally 8 bits).

Walrus Operator

Go has a shorthand for declaring and initializing variables using the := operator, known as the walrus operator. For example: userAge := 30 declares and initializes an integer variable userAge with the value 30. It infers the type based on the assigned value.

When a variable is declared but not initialized, it gets a default value: 0 for numeric types, false for booleans, and "" (empty string) for strings.

TIP

The walrus operator can only be used inside functions, but it should be used whenever possible for brevity.

Types

Go is statically typed, which means all variable types are known before the code runs, which is very nice since it ensures the code is type safe and can catch type-related errors at compile time.

Besides the most basic types that are common to most languages, such as int, string, bool, etc, Go has some unique types for specific use cases: rune (for Unicode code points), byte (alias for uint8), and complex64/128 (for complex numbers) are some examples.

TIP

You should always prefer using the default types (int, float64, etc) unless you have a specific reason to use a different type or size.

NOTE

Notice that you can specify a number along with the type, such as int32 or float64, this controls the size of the type and how much memory (in bits) it occupies. It can be pretty useful when you need to optimize for performance or memory usage.

Packages & Dependencies

To install a package use go get <github-repo>