Learning Golang

Learning Golang

(I might not know what I am talking about)

What is Go?

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

  • Statically typed
  • Simplified concurrency (Goroutines)
  • Fast compiler
  • Read A Tour of Go

Language

Basic types

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

Language

Custom types

type Id = uint64

type ExampleGuy struct {
  isCool       bool
  numberOfCars int32
}

type Something interface {
  calc() float64
}

Language

Slices (Arrays)

var numbers = []uint32{1, 2, 3, 4}

for _, n := range numbers {
  fmt.Println(n)
}

Language

Maps

m := map[uint64]bool{
  1: true,
  2: false,
}

fmt.Println(m[1]) // true
fmt.Println(m[2]) // false

Language

Functions

func Calc(a float64, b float64) float64 {
  return a + b
}

Language

Functions

func Multiply(a string) (float64, error) {
  i, err := strconv.ParseFloat(a, 64)
  if err != nil {
    return 0, err
  }
  return i * i, nil
}

Language

Function receivers

type Measurements struct {
  measures []float64
}

func (m *Measurements) Sum() float64 {
  var result float64
  for _, measure := range m.measures {
    result += measure
  }
  return result
}

var measures = &Measurements{
  measures: []float64{1, 2, 3, 4},
}

measures.Sum()

Language

No classes

There are no classes in Golang. Organize your code in packages

Language

Packages
// main.go
package main

import (
  "github.com/papertrail/sawmill/event"
)
// event/event.go
package event

type Event struct {
  id uint64
}

Language

Concurrency

Goroutines are lightweight threads (green threads) managed by the Go runtime.

func main() {
  go doSomething()
}

Language

Concurrency

Use channels to transport data.

c := make(chan string)
c <- "hello"
str := <-c
fmt.Println(str) // hello

Language

Concurrency

Channels are blocking

c := make(chan string)
str := <-c
fmt.Println(str) // Not happening

Language

Concurrency

Use for and select to read from multiple channels

func (q *Queue) Start() {
  tick := time.NewTicker(time.Second * 10)
  for {
    select {
    case measurement := <-q.insertChan:
      // Add measurement to queue
      break
    case <-tick.C:
      // Flush queue
      break
    }
  }
}
go queue.Start()

Benefits of Go

Easy to learn

A couple weeks to ramp up. VSCode, Goland, others …

Benefits of Go

Performance

Inserting 75k rows per second into ClickHouse

How do you go?

Create main.go

// main.go
package main

import "fmt"

func main() {
  fmt.Println("Hello!")
}

How do you go?

Build it!

$ go build
$ ls -l
total 4128
-rwxr-xr-x  1 lthomas1  staff  2106512 Jan 12 14:38 hello
-rw-r--r--  1 lthomas1  staff       67 Jan 12 14:36 main.go

How do you go?

Run it!

$ ./hello
Hello!

Compiling

Compiles quickly

$ time go build

real	0m0.722s
user	0m0.665s
sys	0m0.385s

Fat binaries

$ ls -lh sockets-talk
-rwxr-xr-x  1 lthomas1  staff   6.6M Jan 12 14:16 sockets-talk

Demo

Source code