Go Cheat Sheet
Table of Contents:
- Basic Syntax
- Operators
- Declarations
- Functions
- Built-in Types
- Type Conversions
- Packages
- Control structures
- Arrays, Slices, Ranges
- Maps
- Structs
- Pointers
- Interfaces
- Embedding
- Errors
- Concurrency
- Printing
- Reflection
- Snippets
Credits
Most example code taken from A Tour of Go, which is an excellent introduction to Go.
If you’re new to Go, do that tour. Seriously.
Go in a Nutshell
- Imperative language
- Statically typed
- Syntax tokens similar to C (but less parentheses and no semicolons) and the structure to Oberon-2
- Compiles to native code (no JVM)
- No classes, but structs with methods
- Interfaces
- No implementation inheritance. There’s type embedding, though.
- Functions are first class citizens
- Functions can return multiple values
- Has closures
- Pointers, but not pointer arithmetic
- Built-in concurrency primitives: Goroutines and Channels
Basic Syntax
Hello World
File hello.go:
$ go run hello.go
Operators
Arithmetic
| Operator |
Description |
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
quotient |
% |
remainder |
& |
bitwise and |
| |
bitwise or |
^ |
bitwise xor |
&^ |
bit clear (and not) |
<< |
left shift |
>> |
right shift |
Comparison
| Operator |
Description |
== |
equal |
!= |
not equal |
< |
less than |
<= |
less than or equal |
> |
greater than |
>= |
greater than or equal |
Logical
| Operator |
Description |
&& |
logical and |
|| |
logical or |
! |
logical not |
Other
| Operator |
Description |
& |
address of / create pointer |
* |
dereference pointer |
<- |
send / receive operator (see ‘Channels’ below) |
Declarations
Type goes after identifier!
Functions
Functions As Values And Closures
Variadic Functions
Built-in Types
All Go’s predeclared identifiers are defined in the builtin package.
Type Conversions
Packages
- Package declaration at top of every source file
- Executables are in package
main
- Convention: package name == last name of import path (import path
math/rand => package rand)
- Upper case identifier: exported (visible from other packages)
- Lower case identifier: private (not visible from other packages)
Control structures
If
Loops
Switch
Arrays, Slices, Ranges
Arrays
Slices
Operations on Arrays and Slices
len(a) gives you the length of an array/a slice. It’s a built-in function, not a attribute/method on the array.
Maps
Structs
There are no classes, only structs. Structs can have methods.
Anonymous structs:
Cheaper and safer than using map[string]interface{}.
Pointers
Interfaces
Embedding
There is no subclassing in Go. Instead, there is interface and struct embedding.
Errors
There is no exception handling. Instead, functions that might produce an error just declare an additional return value of type error. This is the error interface:
Here’s an example:
Concurrency
Goroutines
Goroutines are lightweight threads (managed by Go, not OS threads). go f(a, b) starts a new goroutine which runs f (given f is a function).
Channels
Channel Axioms
-
A send to a nil channel blocks forever
-
A receive from a nil channel blocks forever
-
A send to a closed channel panics
-
A receive from a closed channel returns the zero value immediately
Printing
Reflection
Type Switch
A type switch is like a regular switch statement, but the cases in a type switch specify types (not values) which are compared against the type of the value held by the given interface value.
Snippets
Files Embedding
Go programs can embed static files using the "embed" package as follows:
Full Playground Example
HTTP Server