Built-in types

  • bool

  • string

  • int, int8, int16, int32, int64

  • uint, uint8, uint16, uint32, uint64

  • uintptr

  • byte

  • rune

  • float32, float64

  • complex64, complex128

Declare a variable

var name string

Assign a variable

name = "Bob"

Declare and assign a variable (with type inference)

name := "Bob"

Declare a constant (can be string, char, or numeric types)

const Answer = 42

Convert between types

var f float64 = float64(i)
f := float64

Arrays and Slices

Declare a static array

var coefs [128]int

Dynamically allocate an array and get a pointer

p := new([64]int)

Create slice (dynamically sized array) with length and capacity of 7

s := make([]int, 0, 7)

Create empty slice with capacity of 7

s := make([]int, 7)

Convert entire array to slice (doesn’t copy the underlying array)

a := a[:]

Make a slice from a segment of array or existing slice

s := a[1:4]


Declare a struct type

type Coordinate struct {
Latitude float32
Longitude float32

Instatiate a struct

e := Coordinate{
Latitude: 1.0,
Longitude: 2.0,

Structs can be nested for composition

type Region struct {
NWCorner Coordinate
SECorner Coordinate


main function is a starting point of every program

package main
func main() { ... }

Assign anonymous function to variable

pred := func() bool {
return x > 200

Define a simple function

func functionName() { ... }

Define a function with arguments

func functionName(firstName string, age int) { ... }

Define a function with a return type

func howMuch() int {
return 1

Define function with multiple return types

func getPerson() (string, string) {
return "John", "Smith"

Use named function results

func getPerson() (name string, surname string) {
name = "John"
surname = "Smith"

Flow control


if a <b {
} else if a > b {
} else {

C-style for loop

for i := 1; i < 5; i++ {
sum += i

While loop (use continue to skip to next iteration or break to cancel`)

for i <10 {

Iterative loop

for {


Method-by-value (copies the entire struct to stack, cannot mutate)

package main
import "fmt"
type Person struct {
firstName string
lastName string
func changeName(p Person) {
p.firstName = "Bob"
func main() {
person := Person {
firstName: "Alice",
lastName: "Dow",

Method-by-reference (refers to origin struct through pointer - can mutate)

package main
import "fmt"
type Person struct {
firstName string
lastName string
func changeName(p *Person) {
p.firstName = "Bob"
func main() {
person := Person {
firstName: "Alice",
lastName: "Dow",

Call a method

StackOverflow explanation here.

File 1: main.go (located in MyProj/main.go)

package main
import (
func main(){

File 2: functions.go (located in MyProj/functions/functions.go)

package functions
// `getValue` should be `GetValue` to be exposed to other packages.
// It should start with a capital letter.
func GetValue() string{
return "Hello from this another package"

Learning Resources


Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.


Constants are declared like variables, but with the const keyword.

Constants can be character, string, boolean, or numeric values.

Constants cannot be declared using the := syntax.

Characteristics of a Golang test function

  • The first and only parameter needs to be t *testing.T

  • It begins with the word Test followed by a word or phrase starting with a capital letter.

  • (usually the method under test i.e. TestValidateClient)

  • Calls t.Error or t.Fail to indicate a failure (I called t.Errorf to provide more details)

  • t.Log can be used to provide non-failing debug information

  • Must be saved in a file named something_test.go such as: addition_test.go