Golang

fmt stands for formatted IO

Table of Contents

Resources

Installation

Repositories need to be put into $GOPATH/src

Windows

After installation, set GOPATH environment variable to something like C:\Users\Eddie\Go

Ubuntu

Install with sudo apt-get install golang

Set GOPATH with export GOPATH=$HOME/dev/go

Language

Pointer overview

func main() {
    i, j := 42, 2701

    p := &i         // point to i
    fmt.Println(*p) // read i through the pointer
    *p = 21         // set i through the pointer
    fmt.Println(i)  // see the new value of i

    p = &j         // point to j
    *p = *p / 37   // divide j through the pointer
    fmt.Println(j) // see the new value of j
}

Slices

Reference

  • Declared without element count: letters := []string{"a", "b", "c", "d"}
  • Created using make: s := make([]byte, 5)
  • arr[a:b] takes the ath to the b - 1th element (same as Python slicing)
  • Concating two slices
  • Gotcha regarding append: "The append function appends the elements x to the end of the slice s, and grows the slice if a greater capacity is needed."

Idioms

Use %d works for printing out maps:

asd := make(map[byte]int)
fmt.Printf("%d", asd)

Convert between strings to numbers with strconv:

one, err := strconv.Atoi("1")
oneString := strconv.Itoa(1)

Iterate over a string by converting it into a byte array first:

mystriter := []byte(mystring)
for i, val := range mystriter {
    //...
}

No idioms for reversing a string, but see Rosetta Code

Packages

  • Every Go program is made up of packages
  • Programs start running in package main
  • Import packages with import
  • Package name is the same as last element for the import path
  • Gopath specifies location of your workspace(s)

Glossary

  • rune: Code point (value that corresponds to a symbol in a particular encoding, e.g. Unicode), or as an alias for int32 (source)