Go Language

https://golang.org/ref/spec

Table of Contents

Basic Types and Operators discussion

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128
Precedence    Operator
    5             *  /  %  <<  >>  &  &^
    4             +  -  |  ^
    3             ==  !=  <  <=  >  >=
    2             &&
    1             ||
+    sum                    integers, floats, complex values, strings
-    difference             integers, floats, complex values
*    product                integers, floats, complex values
/    quotient               integers, floats, complex values
%    remainder              integers

&    bitwise AND            integers
|    bitwise OR             integers
^    bitwise XOR            integers
&^   bit clear (AND NOT)    integers

<<   left shift             integer << unsigned integer
>>   right shift            integer >> unsigned integer

==    equal
!=    not equal
<     less
<=    less or equal
>     greater
>=    greater or equal

&&    conditional AND    p && q  is  "if p then q else false"
||    conditional OR     p || q  is  "if p then true else q"
!     NOT                !p      is  "not p"

Program Initialization and execution

package main and func main() {} discussion

A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.

package and import reference

If we need to import two packages whose names are the same, like math/rand and crypto/rand, into a third package, the import declaration must specify an alternative name for at least one of them to avoid a conflict:

import (
    "crypto/rand"
    mrand "math/rand"
)

When needing only the side effect of some pacakges, use blank import:

import _ "image/png"

Package Initialization discussion

Overall Rules
  • Basically, most evaluation order follows the order of declaration in the file.
  • But, if some dependencies are related, the dependencies are resolved first.
  • For the order between files in a package, the order of evaluation depends on the order of how files are provided to go build command.
file1.go
package main

import "fmt"

func init() {
    fmt.Println("init 1")
}
func init() {
    fmt.Println("init 2")
}
func main() {
    fmt.Println("main")
}
file2.go
package main

import "fmt"

func init() {
    fmt.Println("init 3")
}
output.txt
init 1
init 2
init 3
main

Package Naming discussion

if / else reference

package main
import "fmt"
func main() {
    if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }

    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }

    if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
}

for / range reference

package main

func main() {
    i := 1
    // while
    for i <= 3 {
    }
    // classic
    for j := 7; j <= 9; j++ {
    }
    // infinite
    for {
    }
    // can continue
    for n := 0; n <= 5; n++ {
        if n%2 == 0 {
            continue
        }
    }

    nums := []int{2, 3, 4}
    for _, num := range nums {}  // if idx not needed
    for i, num := range nums {}

    kvs := map[string]string{"a": "apple", "b": "banana"}
    for k, v := range kvs {}
    for k := range kvs {}        // just keys
    for i, c := range "go" {}    // 'c' is 'rune' of the string
}

GOPATH reference

The GOPATH environment variable specifies the location of your workspace.

Which things are addressable? discussion

&x           // variable
&a[2]        // elements of array/slice
&person.name // addressable field of a struct
&Point{2, 3} // composite literal
&*p          // pointer indirection

How Go Workspaces(directory structure) are organized tutorial

A workspace is a directory hierarchy with three directories at its root:

bin/
    hello   # command executable
    outyet  # command executable
pkg/
    linux_amd64/
        github.com/golang/example/
            stringutil.a  # package object
src/
  github.com/golang/example/
    .git/           # Git repository metadata
    hello/
      hello.go      # command source
    outyet/
      main.go       # command source
      main_test.go  # test source

  golang.org/x/image/
    .git/
    bmp/
      reader.go
      writer.go

Does Go support Python-like unpacking(or restructuring)? discussion

x := strings.Split(s, sep)
a, b := x[0], x[1]

Representability discussion

x                   T           x is representable by a value of T because

'a'                 byte        97 is in the set of byte values
97                  rune        rune is an alias for int32, and 97 is in the set of 32-bit integers
"foo"               string      "foo" is in the set of string values
1024                int16       1024 is in the set of 16-bit integers
42.0                byte        42 is in the set of unsigned 8-bit integers
1e10                uint64      10000000000 is in the set of unsigned 64-bit integers
2.718281828459045   float32     2.718281828459045 rounds to 2.7182817 which is in the set of float32 values
-1e-1000            float64     -1e-1000 rounds to IEEE -0.0 which is further simplified to 0.0
0i                  int         0 is an integer value
(42 + 0i)           float32     42.0 (with zero imaginary part) is in the set of float32 values