Skip to main content

Go Cheatsheet

Updated Jul 19, 2023 ·

Variables

Use var to declare a variable a and set type to string.

var a string

Declare a variable with initialization.

var a string = "foobar"

Use type inference for variable declaration.

var a = "foobar"

Shortcut variable declaration.

a := "foobar"

Multiple variable assignment.

a, b := "one", "two"

Maps

Declare a map.

var a map[string]string

Initialize a map using make().

a := make(map[string]string)

Initialize a map with values.

a := map[string]string{"k": "v"}

Arrays

Declare an array.

var d [2]int = [2]int{1, 2}

Note that an array is static, which is why we need to define the size.

Slices

Declare a slice. A slice is dynamic, so no need to add a size.

var a []string

Initialize a slice with a fixed size.

a := make([]string, 5)

Initialize a slice with a capacity greater than size.

a := make([]string, 5, 10)

Declare and initialize a slice with values.

a := []int{1, 2, 3, 4, 5, 6, 7}

Multiple Variables

Declare multiple variables together.

var (
a string
b int
)

Structs

Declare a struct type.

type myStruct struct {
c string
}

Create an instance of a struct.

var a myStruct

Initialize a struct.

a := myStruct{c: "a string"}

Initialize a struct with unnamed fields.

a := myStruct{"a string"}

Access and modify struct fields.

a.c = "a string"

Basic Operations

Perform basic arithmetic.

a := 5 + 5

Increment and decrement.

a += 5          // same as a = a + 5
b++ // increase b by one
b-- // decrease b by one

Types

Basic types in Go.

bool
string
byte // alias for uint8
rune // alias for int32
float32
float64
int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
uintptr
complex64
complex128

Conditions

Basic if condition.

if a >= 10 && b >= 10 {
} else if !(a < 5) {
} else {
}

switch statement.

switch a {
case 5:
// do foobar
default: // optional
// do foobar else
}

Operators

Comparison operators.

< > <= >= == !=

Logical operators.

|| && !

Functions

Function with multiple return types.

func returnTwo() (bool, bool) {
// function implementation
}

Function with typed parameters.

func myFunc(b int, c int64) bool {
return true
}

Function with unnamed parameters.

func myFunc(b, c int) {
// function implementation
}

Assigning a function to a variable.

a := func(a int) int { return a + 1 }
fmt.Printf("%v", a(1))

Loops

Traditional for loop.

for i := 0; i < 5; i++ {
fmt.Println(i)
}

Infinite loop: This loop continues indefinitely unless explicitly broken.

for {
fmt.Println("Running")
if a == 5 {
break
}
}

Use continue to skip to the next iteration of the loop.

for {
a++
if a == 5 {
continue
}
fmt.Println(a)
if a > 7 {
break
}
}

Use range to iterate over slices, arrays, maps, etc.

for k, v := range myMap {
fmt.Printf("Key: %v, Value: %v\n", k, v)
}

Methods

Declare a method with a value receiver.

func (m myStruct) method() {
m.c = "newstr" // will not update original
}

Declare a method with a pointer receiver.

func (m *myStruct) method() {
m.c = "newstr" // will update original
}

Interfaces

Declare an interface.

type myIface interface {
method()
method2(a int) (bool, error)
}

Pointers

Declare a pointer.

a := "123"
b := &a // pointer to a
fmt.Println(b) // e.g., 0xc00001c030
fmt.Println(*b) // "123"

Modify the value through the pointer.

*b = "456"       // change a through pointer b

Constants

Declare a constant.

const a int = 5

Goroutines

Run a function concurrently.

go hello()