Golang

Variables

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]

Structs

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
}

Functions

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"
return
}

Flow control

if-then-else

if a <b {
fmt.Println("smaller")
} else if a > b {
fmt.Println("larger")
} else {
fmt.Println("equals")
}

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 {
}

Methods

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",
}
changeName(person)
fmt.Println(person)
}

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",
}
changeName(&person)
fmt.Println(person)
}

Call a method

StackOverflow explanation here.

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

package main
import (
"fmt"
"MyProj/functions"
)
func main(){
fmt.Println(functions.GetValue())
}

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"
}

Notes

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

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.