William Vincent


| Home | Blog | Projects | Contact |

<- blog-index

WIP - Note: A Tour of Go

[tags]:- go, learning, programming, note, WIP

This post is my notes as I read and work through the "tour of go" interactive introduction to golang.



Basics

Packages, variables, functions

Hello

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, 世界")
}

Packages

package main

import (
    "fmt"
    "math/rand" // "math/rand" -> rand
)

func main() {
    fmt.Println("My favorite number is", rand.Int(10))
}

Imports

package main

import (
    "fmt"
    "math"
)

/*
Can also be written as individual line items, the parenthesized "factored" version is preferred

import "fmt"
import "math"
*/

func main() {
    fmt.Printf("Now you have %g problems.\n", math.Sqrt(7))
}

Exported Names

Functions

package main

import "fmt"

func add(x int, y int) int {
  return x + y
}

func main(){
  fmt.Println(add(42, 13))
}
package main

import "fmt"

func add(x, y int) int {
  return x + y
}

func main(){
  fmt.Println(add(42, 13))
}

Multiple Results

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x // multiple results
}

func main() {
    a, b := swap("hello", "world") // grabbing both results
    fmt.Println(a, b)
}

Named Return Values

package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // return (x, y)
}

func main() {
    fmt.Println(split(17)) // prints 7 10
}

Variables

package main

import "fmt"

var c, python, java bool // package level

func main() {
    var i int // function level
    fmt.Println(i, c, python, java)
}

Variables with initializers

package main

import "fmt"

var i, j int = 1, 2

func main() {
    var c, python, java = true, false, "no!"
    fmt.Println(i, j, c, python, java)
}

Short variable declarations

package main

import "fmt"

func main() {
    var i, j int = 1, 2
    k := 3
    c, python, java := true, false, "no!"

    fmt.Println(i, j, k, c, python, java)
}

Basic types

package main

import (
    "fmt"
    "math/cmplx"
)

var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

func main() {
    fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
    fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
    fmt.Printf("Type: %T Value: %v\n", z, z)
}

Zero values

package main

import "fmt"

func main() {
    var i int
    var f float64
    var b bool
    var s string
    fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

Type conversions

package main

import (
    "fmt"
    "math"
)

func main() {
    var x, y int = 3, 4
    var f float64 = math.Sqrt(float64(x*x + y*y)) // long style
    z := uint(f) // short style
    fmt.Println(x, y, z)
}

Type inference

// Typed example

var i int
j := i // j is an int
// Untyped example

i := 42           // int
f := 3.142        // float64
g := 0.867 + 0.5i // complex128
package main

import "fmt"

func main() {
    v := 42 // change me!
    fmt.Printf("v is of type %T\n", v)
}

Constants

package main

import "fmt"

const Pi = 3.14

func main() {
    const World = "世界"
    fmt.Println("Hello", World)
    fmt.Println("Happy", Pi, "Day")

    const Truth = true
    fmt.Println("Go rules?", Truth)
}

Numeric constants

package main

import "fmt"

const (
    // Create a huge number by shifting a 1 bit left 100 places.
    // In other words, the binary number that is 1 followed by 100 zeroes.
    Big = 1 << 100
    // Shift it right again 99 places, so we end up with 1<<1, or 2.
    Small = Big >> 99
)

func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
    return x * 0.1
}

func main() {
    fmt.Println(needInt(Small))
    //fmt.Println(needInt(Big)) will cause crash (an int can store at max a 64-bit integer)
    fmt.Println(needFloat(Small))
    fmt.Println(needFloat(Big))
}

Flow control statements: for, if, else, switch, defer

For

package main

import "fmt"

func main() {
    sum := 0
    for i := 0; i < 10; i++ {
        sum += i
    }
    fmt.Println(sum)
}

More types: structs, slices, and maps

Methods and interfaces

Generics

Concurrency