Go for Scalable Microservices

Why and When you should learn Go?

Iman Tumorang

Senior Software Engineer, Xendit Inc.

Golang

Gophers building a cloud system by Renée French
2

Creators of Go

Robert Griesemer, Rob Pike, Ken Thompson (left to right)
3

Golang

Golang on Google Trend
4

Go in tech Industry

Product built with Go

Companies using Go

5

Why Golang?

6

Problems addressed by Go at Google

7

Dependencies in Go

Moving gopher by Ashley McNamara
8

Unused dependencies

9

Dependencies in Go

comic by xkcd
10

Deployment in Go

comic by kavan
11

Go Syntax

var count int

type Star struct{ brightness int }
func FnThatReturnsMulti(num int) (result int, err error) { return 0, nil }
12

Naming

Exported vs Un-exported

func ThisIsExported() {}

func thisIsNotExported() {}
13

Tooling

14

Is Go for you?

15

Mind Budget

Anything that you cannot do instinctively consumes Mind Budget.

We all have limited Mind Budget.

---

Example:

If you do not know how to ride a bicycle
Learning to drive consumes Mind Budget.

If you already do know how to ride bicycle.
Riding does not consume Mind Budget,
but paying attention to the changing road conditions does.

16

The same with technology

Learning many things is consuming mind budgets.
We can learn everything, but we have a limited time and limited mind budget

Learn it if you need it to build a new things.

17

Choosing the Right Tool

Because I write some stuff API Backend,
I'm expert in Java

I want to write frontends
I can write frontends in Java
So I don't need to learn new programming language

18

Choosing the Right Tool

Because I hammer in nails all day,
I can instinctively wield a hammer.

I want to pull out nails.
I still need to learn about pulling out nails but I don't have to learn a new tool.

19

A tool's purpose

A hammer is good for:
- driving in nails
- breaking things
- striking things

---

Go is good for:
- writing backend servers
- being simple to read and understand
- having built-in concurrency
- being portable across Linux, Windows, Darwin (mac), ARM

20

Go is somewhat familiar

go: fmt.Println("Hello World!)

c: printf("Hello World\n");

java: System.out.println("Hello World!);

javascript: console.log('Hello World!);

ruby: puts 'Hello World'

python 2: print 'Hello World'

python 3: print('Hello World')
21

Go is type-safe

package main

import "fmt"

func main() {
    fmt.Println("go is type-safe (1)")
    var i int = 2
    i = "b"
}

Is javascript type-safe?

In new browser tab, F12 to open developer console:

var i = 2;
i = 'b';
22

Go is type-safe (2)

package main

import "fmt"

type myNum int

func main() {
    fmt.Println("go is type-safe (2)")
    fmt.Println("and very strongly typed")
    var i myNum = 2
    var j int = 3
    i = j
    fmt.Println(i)
}
23

Two sides to a coin

Strong static typing:
- allows comprehensive compile-time checks
- is stable, consistent; predictable at run-time

Strong static typing:
- is inflexible, rigid; requires defining when program is written
- is not dynamic, unchanging; type not replaceable at run-time

---
Know what you want, choose tool candidates
Decide if you wish to consume Mind Budget to learn something new.

24

Go has built-in concurrency

The routine below prints a "+", every second, forever.

func plus() {
    for {
        fmt.Print("+")
        time.Sleep(time.Second)
    }
}

The routine below prints a ".", every second, forever.

func dot() {
    for {
        fmt.Print(".")
        time.Sleep(time.Second)
    }
}
25

Concurrent plus and dot

Modify the code below to run plus and dot concurrently.

package main

import (
	"fmt"
	"time"
)

// 10 OMIT
func plus() {
	for {
		fmt.Print("+")
		time.Sleep(time.Second)
	}
}

// 20 OMIT
func dot() {
	for {
		fmt.Print(".")
		time.Sleep(time.Second)
	}
}

func main() {
    fmt.Println("go concurrency")
    // FIXME:
    // plus()
    // dot()
    fmt.Println("About to execute select{}")
    select {} // wait until Ctrl-C is pressed
}
26

Concurrency: javascript edition

function plus(){
  setInterval( ()=>{console.log('+')}, 1000 );
}

function dot(){
  setInterval( ()=>{console.log('.')}, 1000 );
}

F12 to display web console, copy the above functions.
Execute:

plus()
dot()

((webworker ≈ java thread) = os-level thread) ≠ goroutine

27

How "good" is Go's Concurrency?

Hello world static content http server:

func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
                w.Write([]byte("Hello World!\n"))
        })

        log.Fatal(http.ListenAndServe(":8081", nil))
}
28

How "good" is Go's Concurrency? (2)

Go

Slowest:    0.1247 secs
Fastest:    0.0001 secs
Average:    0.0150 secs
Requests/sec:    27665.5311
Status code distribution:
  [200]    1000000 responses

Node

Slowest:    0.2949 secs
Fastest:    0.0003 secs
Average:    0.0150 secs
Requests/sec:    9129.3825
Status code distribution:
  [200]    1000000 responses
29

How "good" is Go's Concurrency? (3)

ab -n1000 -c100 http://127.0.0.1:8081/
Requests per second:    11100.87 [#/sec] (mean)

---

nginx serving "Hello World!":

ab -n1000 -c100 http://127.0.0.1/
Requests per second:    9644.04 [#/sec] (mean)
30

Go is portable

If your go code does not reference any platform specific features;
(eg. Load Windows DLLs)

and does not use c-libraries (which are compiled for specific platforms);

you can:

CGO_ENABLED=0 GOOS=linux GOARCH=386 go build helloWorld.go

See the complete list here:

31

Takeaways

32

Discussion Time!

33

Thank you

Iman Tumorang

Senior Software Engineer, Xendit Inc.

twitter.com/bxcodec

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)