Go a tour

[WIP]

 

Andy Truong

@thehongtt

 

December 2015

Program structure

// @file anything.go — play.golang.org/p/t9pJKg1gml

package main

import "fmt"

func main() {
    fmt.Println("Hello world!")
}
go run anything.go

Basic controller

import

import "fmt"

// Single line importing
import ("net/http", "net/url")

// Prefered way
import (
    // Core packages
    "encoding/json"

    // Importing groups
    atbar "github.com/andytruong/foo/bar"
)

function

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

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

// Short return
func add(x, y) (z int) {
    z = x + y
    return
}
// Return multiple values
func swap(x, y int) (int, int) {
    return y, x
}

// Return multiple values + short return
func split(sum int) (x, y int) {
  x = sum * 4 / 9
  y = sum - x
  return
}

constant

// classic
const f = "%T(%v)\n"

// Multiple
const (
  Big = 1 << 100
  Small = Big >> 99
)

variable

// Not initialized value. Default is false
var c, python, java bool

// With init value
var i, j int = 1, 2

// Short syntax
age := 32

if/else

if true {
    fmt.Println("True")
}

if true {
    fmt.Println("True")
} else {
    fmt.Println("False")
}

// If with short statement
// isTrue is only available inside if block
if isTrue := true; isTrue {
    fmt.Println("True")
}

switch

// No break
switch runtime.GOOS {
case "darwin":
  fmt.Println("OS X.")
case "linux":
  fmt.Println("Linux.")
default:
  fmt.Printf("%s.", runtime.GOOS)
}

// No input
switch {
case true:
  fmt.Println("True")
case false:
  fmt.Println("False")
}

loop

// Basic for
sum := 0
for i := 0; i < 10; i++ {
	sum += 1
}

// For with only condition checking
sum := 1
for sum < 1000 {
	sum += sum
}

// For without statement
sum := 1
for {
	sum += sum
	if sum > 1024 {
		break
	}
}

defer

func main() {
    defer fmt.Println("3")
    defer fmt.Println("2")
    fmt.Println("1")
}

Data

Basic data types

  1. bool
  2. string
  3. int int8 int16 int32 int64
  4. uint uint8 uint16 uint32 uint64 uintptr
  5. byte // alias for uint8
  6. rune // alias for int32, represents a Unicode code point
  7. float32 float64
  8. complex64 complex128

Type casting

i := 42
f := float64(i)
u := uint(f)

Custom data type

type SuperString string

func main() {
  s := SuperString("My string")
  fmt.Println(s)
}

Struct

type person struct {
    name string
    year int
}

type Vertex struct {
    x, y int
}

v1 := Vertex{1, 2} // {1 2}
v2 := Vertex{y: 2} // {0 2}
v3 := Vertex{}     // {0 0}

Pointer

// Define a pointer to a type
var p *int

// Short syntax
i := 1
p := &i

// Pointer access
*p += 1; fmt.Println(*p)
// Define
var a  [2]string

// Write
a[0] = "Hello"
a[1] = "world!"

// Read
fmt.Println(a[0], a[1])
// Define
var a  [2]string

// Write
a[0] = "Hello"
a[1] = "world!"

// Read
fmt.Println(a[0], a[1])
var s  []int{1, 2, 3, 4, 5}
var ss [][]string{
  []string{"x", "_", "_"},
  []string{"_", "x", "_"},
  []string{"_", "_", "x"},
}

// Access
s[1:2]
s[:3]
s[3:]

// Look
for k, v := range s {
    fmt.Println(k, v)
}

// Exercise: Print ss

Map

Closure

Data+

Coroutine

Coroutine+

Package

Dependency management

Reflection

Testing

Convey

Next

  • Write some command line
  • Gin web framework

Resources

Go a tour

By Andy Truong

Go a tour

  • 1,398