Slide 1

Slide 1 text

Golang for C developers Federico Paolinelli List

Slide 2

Slide 2 text

About me Lead developer at List C on daily basis Things I tinker with in my spare time: Android Python

Slide 3

Slide 3 text

Let's talk about C

Slide 4

Slide 4 text

Very simple You have Structs struct Book { char title[50]; char author[50]; }; Book b; // a mess memset(&b, 0, sizeof(Book)); strcpy(b.title, "Golang"); and functions void printBook(struct Book book) { printf("Title: %s", book.title); printf("Author: %s", book.author); }

Slide 5

Slide 5 text

Oh, and you have pointers int i = 5; void changeValue(int* i) { *i = 12; } int array[3] = {0, 1, 2}; printf("%d", *(array + 2 * sizeof(int)); // 2 Direct access to memory locations. It's only way to pass references around instead of copies.

Slide 6

Slide 6 text

What I like about C fast direct control of things (pointers!) no hidden costs you understand what you are reading

Slide 7

Slide 7 text

What I don't like about C verbose easy to shoot on your foot limited standard library dependency management

Slide 8

Slide 8 text

The recipe for Go

Slide 9

Slide 9 text

The recipe for Go Go back to the 70s / 80s and nd a bunch of great programmers. Say, Ken Thompson, Rob Pike, people like that. Marinate them in Bell Labs for 30 years, during which time they code in C, keep developing Unix, invent Plan 9, UTF-8, and other wonderful things. Take them out, seduce them with Python, wow them with Google-scale computing. Add more amazing programmers (Brad Fitzpatrick for example), stir in Google’s near- unlimited resources. Ask them how they would do C now, if they could start from scratch. credits by Graham King (https://www.darkcoding.net/software/go-lang-after-four-months/)

Slide 10

Slide 10 text

Can a language be fast and fun to use?

Slide 11

Slide 11 text

Go's elevator pitch: fast (C-ish fast) good at networking and multiprocessing scales well easy to learn comprehensible

Slide 12

Slide 12 text

Let's dive in

Slide 13

Slide 13 text

Basic data types As in C, we do have integers, oats, booleans: var n int64 = 23 var n1 int = 28 var p float64 = 9.75 var r bool = true Go provides a higher level string type: var s string = "Hello" s = s + " Devfest" // s = "Hello devfest" s = s[1:5] // ello

Slide 14

Slide 14 text

Arrays in C: int numbers[10]; for (int i = 0; i < 10; i++) numbers[i] = 0; length is not a property shortcut for pointer arithmetic (non initialized, out of bound errors write in memory) Arrays in Go: var a[3]int = [3]int{1,2,3} var b[3]int = [...]int{1,2,3} a[2] // 3 len(a) // 3 Fixed lenght Passed by value

Slide 15

Slide 15 text

Slices var a s[]int = []int{1,2,3} s1 := a[1:3] // 2, 3 s2 := make([]int, 3) // {0, 0, 0} var s3 s := append(s, 4) / s := append(s, s1...) // 1,2,3,4,2,3 Dynamically sized Automatically resized by append

Slide 16

Slide 16 text

Maps var values = make(map[string]int) values["pr"] = 23 values["bdu"] = 28 delete(m, "bdu") if age, ok := values["pr"]; !ok { /* ... */ } // if not found, age = 0 value no native equivalent in C

Slide 17

Slide 17 text

Functions func f(a, b int) (string, error) { /* ... */ } multiple return values (error handling) rst class values can be anonymous func getAdder(toSum int) func(int) int { return func(arg int) int { return arg + toSum; } }

Slide 18

Slide 18 text

Memory management

Slide 19

Slide 19 text

Memory management in C heap vs stack int* bird = malloc(sizeof(int)) // lives in the heap, need to be freed int* f() { int b = 5; return &b; // BAD! } e cient the dev is in control of what lives in the stack and what in the heap easy to forget the lifecycle of malloc'ed objects

Slide 20

Slide 20 text

Memory management in C

Slide 21

Slide 21 text

Memory management in Go There are pointers too, which is good: the dev is in control of what is passed by value and what by reference func f() int* { int result = 28 return &result //result stored in the heap } garbage collected pointers are references to live objects: there is no way to mess up with the memory

Slide 22

Slide 22 text

But I really really miss pointer arithmetic!

Slide 23

Slide 23 text

Unsafe package The package name is self explanatory. It provides Pointers. A pointer value of any type can be converted to a Pointer. A Pointer can be converted to a pointer value of any type. func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) } Pointer arithmetic f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f))

Slide 24

Slide 24 text

Nice things about GO

Slide 25

Slide 25 text

It's safer Prevents side e ects related to distractions assignement between di erent types requires explicit conversion int n = 1400.5 / 'c'; // ????? variables are initialized with zero values MyStruct* s = (MyStruct*) malloc(sizeof(MyStruct)); memset(s, 0, sizeof(MyStruct)); no pointer arithmetic MyStruct s; int* i = (&s + 3 * sizeof(int)); // ????

Slide 26

Slide 26 text

Doing more with (a bit) less No ; nor () around ifs Type inference, short variable declaration var s = "devfest" s1 := "devfest" // only inside a function Multiple return values (also, named return values) func f(arg int) (res int, err error) { /* ... */ }

Slide 27

Slide 27 text

Doing more with less Range loops (in maps, slices and strings) for index , _ := range(mySlice) { /* ... */ } defer() to call nalizing operations inside a function no break in switch, switch cases can be conditions

Slide 28

Slide 28 text

Productivity boost! Compiling is really fast

Slide 29

Slide 29 text

Let's talk about OOP

Slide 30

Slide 30 text

User de ned data structures C structs: struct Book { char title[50]; char author[50]; }; Book b; // a mess memset(&b, 0, sizeof(Book)); strcpy(b.title, "Golang"); Go Structs type book struct { title string author string } var b book // zero value var b1 book{title:"Golang"} // literal initializer

Slide 31

Slide 31 text

Methods Just a special kind of functions (they have a receiver) Explicitly associated to objects or pointers type Dog struct { Animal } func (d Dog) bark() { // bark } func (d *Dog) feed() { d.weight++ }

Slide 32

Slide 32 text

Interfaces Abstract types de ning a behaviour Satis ed implicitly type Driveable interface { func Drive() int } func (c Car) Drive (km int) {/* */} func (b Bike) Drive (km int) {/* */} var c Car{wheels : 4} var d Driveable = c Can be checked c, ok := d.(Car) // true c, ok := d.(Bike) // false switch d.(type) { case Car: // }

Slide 33

Slide 33 text

Encapsulation

Slide 34

Slide 34 text

Encapsulation in C static modi er for functions and variables Local to a .c le Directly related to exported names available during linking

Slide 35

Slide 35 text

Encapsulation in Go: The visibility is related to the package Multiple les can belong to the same package The visibility is toggled by uppercasing / lowercasing Every le belongs to a package package mypackage var Exported int var notExported string

Slide 36

Slide 36 text

The worst nightmare of every (C) programmer

Slide 37

Slide 37 text

Knock knock. Race condition. Who's there?

Slide 38

Slide 38 text

pthread.h Provide primitives to create and synchronize threads together. Mutexes Condition variables Semaphores Multithreading is today's goto : - hard to read / understand - di cult to debug What we need is a better solution for concurrency, easier to understand and to handle

Slide 39

Slide 39 text

Enters goroutine Goroutines are lightweight threads managed by the Go runtime. go myFunction() Why lighter? smaller (altough variable) stack many goroutines can be share a single os thread scheduling not invoked periodically but as a consequence of synchronization It is practical to create hundreds of thousands of goroutines in the same address space

Slide 40

Slide 40 text

Why are they di erent?

Slide 41

Slide 41 text

Channels A channel is a communication mechanism between two goroutines ch := make(chan int) We can send values to a channel ch <- 23 or receive from a channel res := <- ch or just check if send has happened <- ch Channels can be bu ered or unbu ered. Sending on a full channel blocks the sender, receiving from an empty channel blocks the receiver.

Slide 42

Slide 42 text

Goroutines are indipendent executing actors communicating via channels De netely easier to reason about

Slide 43

Slide 43 text

An example package main import "fmt" func main() { source := make(chan int) incremented := make(chan int) go func() { for i := 0; i < 100; i++ { source <- i } close(source) }() go func() { for s := range source { incremented <- s + 1000 } close(incremented) }() for res := range incremented { fmt.Println(res) } } Run

Slide 44

Slide 44 text

Where are my mutexes?

Slide 45

Slide 45 text

Go provides synchronization primitives Mutexes RW Locks sync.Once

Slide 46

Slide 46 text

Everything else

Slide 47

Slide 47 text

Coding style Survey by stackover ow (https://stackover ow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/) Go fmt formats the code in the "go way" easier to read easier to write no tabs vs spaces discussions!

Slide 48

Slide 48 text

Building Workspace: root tree pointed by $GOPATH variable bin/ hello # command executable pkg/ linux_amd64/ github.com/fedepaol/example/ stringutil.a # package object src/ github.com/fedepaol/example/ hello/ hello.go # command source stringutil.go golang.org/x/image/ bmp/ reader.go # package source writer.go # package source Most Go programmers keep all their Go source code and dependencies in a single workspace

Slide 49

Slide 49 text

Building (2) go build package non main packages are compiled and thrown away executables are compiled and left in the current dir go install package non main packages are deployed in $GOPATH/pkg executables in $GOPATH/bin go get package (as in github.com/fedepaol/example/hello) downloads the package and its dependencies installs it

Slide 50

Slide 50 text

Deploying There is no need for shared libraries. Not even libc. The go runtime is linked together with the executable. The absence of external dependencies makes the deployement a lot easier than a C executable.

Slide 51

Slide 51 text

There's more! re ection race detector C interoperability rich standard library awesome echosystem (https://github.com/avelino/awesome-go) integrated testing framework native cpu and memory pro ling cross compiles awesome linters

Slide 52

Slide 52 text

What I did expect from Go

Slide 53

Slide 53 text

Doing a lot with few lines of code Javascript var result = tasks.map(function (task) { return (task.duration / 60); }).filter(function (duration) { return duration >= 2; }); Java Arrays.stream(myArray) .filter(s -> s.length() > 4) .map(s -> s.toUpperCase()) .toArray(String[]::new); Kotlin strings.filter { it.length == 5 }.sortedBy { it }.map { it.toUpperCase() }

Slide 54

Slide 54 text

What I found Go focuses on being explicit rather than implicit being readable being safe getting sh*t done

Slide 55

Slide 55 text

Where to go from here O cial website golang.org (http://golang.org) Interactive Tutorial tour.golang.org (http://tour.golang.org) E ective go golang.org/doc/e ective_go.html (https://golang.org/doc/e ective_go.html) Mailing list groups.google.com/forum/#%21forum/golang-nuts (https://groups.google.com/forum/#%21forum/golang-nuts)

Slide 56

Slide 56 text

Thank you Federico Paolinelli List @fedepaol (http://twitter.com/fedepaol) [email protected] (mailto:[email protected])

Slide 57

Slide 57 text

No content