Introduction to Golang

Scratching the surface

3 December 2019

Jerry Jacobs

Software Engineer, Heliox

Welcome!

2

Introducing myself

3

Agenda

4

History

5

Intro: Why a new language?

6

Language features

7

Modern software practices

The language and toolchain contains support for modern programming practices

8

Pros and cons compared to C/C++

Pros

Cons

9

Pros and cons compared to C/C++

C/C++ is just a language specification, needs many tools

Go has just a single tool

go
10

Who uses it?

11

Language howto

12

standard library

Go has an excellent standard library

13

Language Organization

14

Hello World!


 1// All go files must start with a package name
 2//  the "main" package is where the main function resides
 3package main
 4
 5// The import statement is for loading other packages
 6//  the "fmt" package is for formatting
 7import (
 8	"fmt"
 9)
10
11// The main function is the entrypoint of the executable
12func main() {
13	fmt.Println("Hello World!")
14}

Compile and run:

$ go run hello.go
15

Values and variables


 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	// Concatenate and calculate
 7	fmt.Println("foo" + "bar")
 8	fmt.Println("12.3 * 2 =", 12.3*2)
 9
10	// Variables always have a initialized value
11	var a uint
12	fmt.Println("a =", a)
13
14	// Multiple variables with multiple types
15	var b, c = 10.5, "chicken"
16	fmt.Printf("b(%v): %T, c(%v): %T\n", b, b, c, c)
17
18	// Single variable declare and assign
19	d := "var d"
20	fmt.Println(d)
21}
16

Flow control: for


 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	// A classical for loop which counts from 1 to 3
 7	for n := 1; n < 4; n++ {
 8		fmt.Println("n =", n)
 9	}
10
11	// Go doesn't have a while statement, for is used
12	sum := 1
13	for sum < 10 {
14		sum++
15	}
16	fmt.Println("sum =", sum)
17}
17

Flow control: if/else


 1package main
 2
 3import (
 4	"fmt"
 5	"time"
 6)
 7
 8func main() {
 9	// Variables declared inside an if are also available in the else
10	if name, offset := time.Now().Zone(); offset == 3600 {
11		fmt.Println("We are here at UTC + 1 hour! (name: ", name, ")")
12	} else if offset == 7200 {
13		fmt.Println("We are here at UTC + 2 hour! (name: ", name, ")")
14	} else {
15		fmt.Println("We are at", name)
16	}
17}
18

Flow control: switch


 1package main
 2
 3import (
 4	"fmt"
 5	"time"
 6)
 7
 8func main() {
 9	// Yes the day of the course is Tuesday
10	switch time.Tuesday {
11	// You can use commas in a case to cover multiple statements
12	case time.Tuesday, time.Friday:
13		// By default, go switch-cases don't fallthrough
14		fallthrough
15	case time.Saturday:
16		fmt.Println("Today is the big day")
17	default:
18		fmt.Println("Its a regular day")
19	}
20}
19

Data structures: array & slice


 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	// An array is a fixed-size object
 7	wordarr := [4]byte{0xde, 0xad, 0xbe, 0xef}
 8
 9	// Convert the array to a slice and typecast to string
10	wordsli := wordarr[:]
11
12	// Print the length and capacity of the slice (and hex)
13	fmt.Printf("len(%d), cap(%d), %x\n", len(wordsli), cap(wordsli), string(wordsli))
14}
20

Data structures: struct


 1package main
 2
 3import "fmt"
 4
 5// Create a struct to store person information
 6type Person struct {
 7	Name string
 8	Age  int
 9}
10
11func main() {
12	// Create and initialize a person
13	p := Person{Name: "Jerry", Age: 30}
14
15	// Print struct including property names
16	fmt.Printf("%+v\n", p)
17}
21

Data structures: map


 1package main
 2
 3import "fmt"
 4
 5type Location struct {
 6	Latitude, Longitude float64
 7}
 8
 9func main() {
10	// Allocate an empty map with make
11	m := make(map[string]Location)
12
13	// Store the location of Heliox
14	m["Heliox"] = Location{
15		51.485945, 5.392263,
16	}
17
18	// Test and get the value of key "Heliox"
19	loc, ok := m["Heliox"]
20	fmt.Println(loc, ok)
21}
22

Pointers


 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	i := 42
 7
 8	p := &i         // point to i
 9	fmt.Println(*p) // read i through the pointer
10	*p = 21         // set i through the pointer
11	fmt.Println(i)  // see the new value of i
12}
23

Methods & interfaces


 1package main
 2
 3import (
 4	"io"
 5	"os"
 6	"strings"
 7)
 8
 9// Read data from r and write to os.Stdout
10//  the io.Reader is an interface with only a Read method
11func WriteToStdout(r io.Reader) {
12	// Copy to an io.Writer (dest) from an io.Reader (source)
13	//  this can be anything! File, network socket etc.
14	io.Copy(os.Stdout, r)
15}
16
17func main() {
18	// Create an io.Reader from a string and print it to os.Stdout
19	sr := strings.NewReader("Hello string reader!")
20	WriteToStdout(sr)
21}
24

Workshop & Q&A

25

Ideas?

Playing around on play.golang.org

26

More information

27

More information (2)

28

Thank you