🌱 Tim's Dev Wiki

Search IconIcon to open search

Go

Last updated September 21, 2022.

Go is a statically-typed compiled language inspired by C in design, but aims to provide memory safety, ease of usage, and the high performance of close-to-metal languages like C++ and Rust, which Go is often compared to. The main overarching difference is that Go makes language design decisions that favour ease of usage over speed.

Notably, Go is credited for improving the developer experience in writing concurrent code with concurrency constructs built into the language. It’s also credited with very fast compilation/build times, which is one reason why Go is prevalent in DevOps.

Go is general-purpose, but it’s typically used for building:


I learned Go mainly from the official ‘A Tour of Go’ tutorial and official docs.

# Packages

All Go programs consist of packages, which consist of source files defining a bunch of functions, variables, etc. Program execution starts in the main package.

Outside of the standard library, there is a rich Go packages ecosystem searchable at pkg.go.dev. This is an awesome community list of packages.

# Imports

Import statements like import fmt creates a binding fmt that lets you access the package’s functions.

# Exports

A binding is exported if it starts with a capital letter, otherwise, it remains only accessible within its package. It’s that simple.

# Variables

Variables are declared with var and can be done at the package-level or function-level.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Package-level variables.
var foo string = "Hi"

func main() {
	// Function-level variables.
	var bar int = 42

	// An equivalent short-hand syntax using :=
	// Note how the type of `baz` is inferred.
	baz := 24
}

# const

Like in JavaScript, you can make bindings const which prevents them from being reassignable after being declared.

1
const Pi = 3.14

# Type System

Go is statically-typed, so the type of every symbol should be known at compile-time.

# Primitive Types

Go’s primitive types include: bool, string, int (and all its variants like uint and int64), float32,float64, complex64 and complex128 for complex numbers.

# Functions

Functions declarations look like this in Go.

1
2
3
func foo(a int) int {   // 
	return a + 42;
}

# Multiple Return Values

Unlike most languages, you can return multiple values without needing to wrap it in a data structure.

1
2
3
4
5
6
7
func foo() (int, int) {
	return 42, 24
}

func main() {
	a, b := foo()    // a → 42, b → 24.
}

# Basic Constructs

# Looping

All looping is done with for. You never use parentheses.

1
2
3
4
5
6
7
8
9
// Regular for-loop.
for i := 1; i < 42; i++ {
	// ...
}

// While loops.
for i < 42 {
	// ...
}

# If-Else

Go has regular if, else if, else like most languages, but you never use parentheses.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func main() {
	theme := "dark"
	if theme == "dark" {
		fmt.Println("🌙")
	} else if theme == "light" {
		fmt.Println("☀️")
	} else {
		fmt.Println("🎨")
	}
}

# Switch

Go has a switch-case construct like most languages, but it’s unnecessary to break after each case to prevent ‘running through’ each case.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
switch theme {
	case "dark":
		fmt.Println("🌙")
	case "light":
		fmt.Println("☀️")
	default:
		fmt.Println("🎨")
}

// Switch-case blocks don't need a switch condition. You can omit it to write a cleaner
// version of a long if-else-if sequence.
switch {
	case x > 0:    // if 
		// ...
	case x < 0:    // else if
		// ...
	default:       // else
		// ...
}