golang 101 (concurrency vs parallelism)

76
fmt.Printf(“Hello, UNSADA”)

Upload: pramesti-hatta-k

Post on 05-Apr-2017

37 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Golang 101 (Concurrency vs Parallelism)

fmt.Printf(“Hello, UNSADA”)

Page 2: Golang 101 (Concurrency vs Parallelism)

Pramesti Hatta K.

Software Engineer @Tech in Asia

Page 3: Golang 101 (Concurrency vs Parallelism)

Making things look hard is easy. Making hard things look easy, that’s hard.

Page 4: Golang 101 (Concurrency vs Parallelism)
Page 5: Golang 101 (Concurrency vs Parallelism)

Meet Gopher, the funniest programming language mascot.

Page 6: Golang 101 (Concurrency vs Parallelism)
Page 7: Golang 101 (Concurrency vs Parallelism)

2 types of programming language

compiled | interpreted

Page 8: Golang 101 (Concurrency vs Parallelism)

compiled languages are converted directly into machine code

Page 9: Golang 101 (Concurrency vs Parallelism)

In contrast to compiled languages, interpreted languages do not require machine

code in order to execute the program; instead, interpreters will run through a program line by line and execute each

command.

Page 10: Golang 101 (Concurrency vs Parallelism)
Page 11: Golang 101 (Concurrency vs Parallelism)

compiled

● often faster execution time

● often slower development time

C/C++, Go, Fortran, Pascal etc

interpreted

● often slower execution time

● often faster development time

PHP, Python, Ruby, JavaScript

Page 12: Golang 101 (Concurrency vs Parallelism)

statically typed

package main

import “fmt”

func main() { var person string

person = “Maudy Ayunda” fmt.Println(person) person = 22 fmt.Println(person)}

Output:cannot use 22 (type int) as type string in assignment

dynamically typed

<?php$person = “Maudy Ayunda”;echo $person;

$person = 22;Echo $person;

Output:Maudy Ayunda22

Page 13: Golang 101 (Concurrency vs Parallelism)
Page 14: Golang 101 (Concurrency vs Parallelism)

New programming language?

Page 15: Golang 101 (Concurrency vs Parallelism)

2007

Page 16: Golang 101 (Concurrency vs Parallelism)

Robert Griesemer, Rob Pike and Ken Thompson

Page 17: Golang 101 (Concurrency vs Parallelism)

2009 (became open source)

Page 18: Golang 101 (Concurrency vs Parallelism)

2012 (finally stable v.)

Page 19: Golang 101 (Concurrency vs Parallelism)

why?

Page 20: Golang 101 (Concurrency vs Parallelism)

Go was born out of frustration with existing languages and environments for systems

programming.

Page 21: Golang 101 (Concurrency vs Parallelism)

one had to choose either efficient compilation, efficient execution, or ease of

programming

Page 22: Golang 101 (Concurrency vs Parallelism)

programmers who could were choosing ease over safety and efficiency by moving to

dynamically typed languages such as Python and JavaScript rather than C++ or, to a

lesser extent, Java.

Page 23: Golang 101 (Concurrency vs Parallelism)

Go is an attempt to combine the ease of programming of an interpreted, dynamically

typed language with the efficiency and safety of a statically typed, compiled language.

Page 24: Golang 101 (Concurrency vs Parallelism)

designed by Google to solve Google’s problem.

Page 25: Golang 101 (Concurrency vs Parallelism)

and Google has big problems.

Page 26: Golang 101 (Concurrency vs Parallelism)

Big hardware

Page 27: Golang 101 (Concurrency vs Parallelism)

Big software

● C++ (mostly) for servers, plus lots of java and python

● thousands of engineers● gazillions of lines of code

Page 28: Golang 101 (Concurrency vs Parallelism)

development at Google can be slow, often clumsy.

Page 29: Golang 101 (Concurrency vs Parallelism)

Goals

● eliminate slowness● eliminate clumsiness● improve effectiveness

Page 30: Golang 101 (Concurrency vs Parallelism)

Go’s purpose is not research into programming language design

Page 31: Golang 101 (Concurrency vs Parallelism)

Go’s purpose is to make software engineer’s lives better.

Page 32: Golang 101 (Concurrency vs Parallelism)

Go Programming Language

● open source● concurrent● garbage collected● efficient● simple● fun● boring (to some)

https://golang.org

Page 33: Golang 101 (Concurrency vs Parallelism)

Go is a statically typed compiled language.

Page 34: Golang 101 (Concurrency vs Parallelism)

Golang is a simple language. It has only 25 keywords.

Page 35: Golang 101 (Concurrency vs Parallelism)
Page 36: Golang 101 (Concurrency vs Parallelism)

Go keywordsbreak default func interface selectcase defer go map structchan else goto package switchconst fallthrough if range typecontinue for import return var

Page 37: Golang 101 (Concurrency vs Parallelism)
Page 38: Golang 101 (Concurrency vs Parallelism)

Yea I’m ready to learn this language

Page 39: Golang 101 (Concurrency vs Parallelism)

Basic structure1.package main

2.

3.import “fmt”

4.

5.func main() {

6. fmt.Printf(“Hello, world.\n”)

7.}

Page 40: Golang 101 (Concurrency vs Parallelism)

Package1.package main

2.

3.import (

4. “fmt”

5. “math/rand”

6.)

7.

8.func main() {

9. fmt.Println("My favorite number is", rand.Intn(10))

10.}

Page 41: Golang 101 (Concurrency vs Parallelism)

Function1.package main

2. …

3.func add(x int, y int) int {

4. return x + y

5.}

6.

7.func main() {

8. fmt.Println(add(22, 8))

9.}

Output30

Page 42: Golang 101 (Concurrency vs Parallelism)

Function with multiple return1.package main

2. …

3.func swap(x, y string) (string, string) {

4. return y, x

5.}

6.

7.func main() {

8. a, b := swap(“hello”, “world”)

9. fmt.Println(a, b))

10.}

Outputworld hello

Page 43: Golang 101 (Concurrency vs Parallelism)

Variables1.package main

2. …

3.var x int

4.

5.func main() {

6. var y string

7. y = "Hi!"

8. fmt.Println(y)

9. fmt.Println(x)

10.}

OutputHi!0

Page 44: Golang 101 (Concurrency vs Parallelism)

Short declaration variables1.package main

2. …

3.

4.func main() {

5. y := “Hi!”

6. fmt.Println(y)

7.}

OutputHi!

Page 45: Golang 101 (Concurrency vs Parallelism)

Basic typesbool

string

int int8 int16 int32 int64

uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32

float32 float64

complex64 complex128

Page 46: Golang 101 (Concurrency vs Parallelism)

Looping1.package main

2. …

3.

4.func main() {

5. for i := 1; i <= 10; i++ {

6. fmt.Println(i)

7. }

8.}

Output12345678910

Page 47: Golang 101 (Concurrency vs Parallelism)

Looping continued1.package main

2. …

3.

4.func main() {

5. i := 1

6. for i <= 10 {

7. fmt.Println(i)

8. i++

9. }

10.}

Output12345678910

Page 48: Golang 101 (Concurrency vs Parallelism)

If and else1.package main

2. …

3.

4.func main() {

5. i := 22

6. if i > 10 {

7. fmt.Println(“big”)

8. } else {

9. fmt.Println(“low”)

10. }

11.}

Outputbig

Page 49: Golang 101 (Concurrency vs Parallelism)

Struct1.package main

2. …

3.

4.type PersegiPanjang struct {

5. P, L int

6.}

7.func main() {

8. fmt.Println(PersegiPanjang{P: 10, L: 22})

9.}

Output{10 22}

Page 50: Golang 101 (Concurrency vs Parallelism)

Array, slice, map, methods, pointer, interfaces etc.

https://tour.golang.org/list

Page 51: Golang 101 (Concurrency vs Parallelism)
Page 52: Golang 101 (Concurrency vs Parallelism)

What the heck is concurrency

Page 53: Golang 101 (Concurrency vs Parallelism)

concurrency

concurrenc

y

concurrenc

y

concurrenc

y concurre

nc

y

concurrenc

y

concurrencyconcurrency

concurrenc

y

concurrenc

y

concurrencyconcurrenc

y

concurrenc

y concurrenc

y

concurrenc

y

concurrency con

curren

c

y

concurrency

Page 54: Golang 101 (Concurrency vs Parallelism)

People will tend to say ...

concurrency is doing two or more things simultaneously,

parallelism is doing two or more things simultaneously.

Page 55: Golang 101 (Concurrency vs Parallelism)

concurrency is not parallelism

Page 56: Golang 101 (Concurrency vs Parallelism)

concurrency

“Gue lagi ngobrol sambil ngopi”

parallelism

“Gue lagi ngoding sambil denger musik”

Page 57: Golang 101 (Concurrency vs Parallelism)

concurrency is the composition of independently executing processes

Page 58: Golang 101 (Concurrency vs Parallelism)

parallelism is the simultaneous execution of (possibly related)

computations

Page 59: Golang 101 (Concurrency vs Parallelism)

concurrency is about dealing with lots of things at once. parallelism is about doing lots of things at

once

Page 60: Golang 101 (Concurrency vs Parallelism)

concurrency is about the structure. while parallelism is about the

execution.

Page 61: Golang 101 (Concurrency vs Parallelism)

but, concurrency can enable parallelism

Page 62: Golang 101 (Concurrency vs Parallelism)

how?

Page 63: Golang 101 (Concurrency vs Parallelism)

to have true parallelism you need to run your program on a machine with

multiple physical processors

Page 64: Golang 101 (Concurrency vs Parallelism)

You need analogies

Page 65: Golang 101 (Concurrency vs Parallelism)

Imagine you have a restaurant

you have no employee

You only have one stove

there is one customer

she orders “omelette” and “french fries”

Page 66: Golang 101 (Concurrency vs Parallelism)

Without concurrency

1.you go to the kitchen

2.you make omelette

3.when omelette is done, give it to the customer

4.you go back to the kitchen

5.you make french fries

6.when french fries is done, give it to the customer

Page 67: Golang 101 (Concurrency vs Parallelism)

With concurrency

1.you go to the kitchen

2.you ask your employee to cook french fries

3.you make omelette OR your employee make french fries

4.when omelette/french fries is done, give it to the customer

NOTE: THIS TIME YOU HAVE ONE EMPLOYEE BUT YOU ONLY HAVE ONE STOVE

Page 68: Golang 101 (Concurrency vs Parallelism)

With concurrency + parallelism

1.you go to the kitchen

2.you ask your employee to cook french fries

3.you make omelette AND your employee make french fries

4.when omelette/french fries is done, give it to the customer

NOTE: THIS TIME YOU HAVE ONE EMPLOYEE AND YOU HAVE TWO STOVES

Page 69: Golang 101 (Concurrency vs Parallelism)
Page 70: Golang 101 (Concurrency vs Parallelism)

1 2 3 4 5 6 7 8 9 10

DOCTOR

IN (4 MINUTES)

CHECK-UP (5 MINUTES)

TIME REQUIRED FOR 1 PERSON4 + 5 = 9 MINUTES

TIME REQUIRED FOR ALL PERSON9 * 10 = 90 MINUTES

WITHOUT CONCURRENCY

Page 71: Golang 101 (Concurrency vs Parallelism)

1 2 3 4 5 6 7 8 9 10

DOCTOR

IN (4 MINUTES)

CHECK-UP (5 MINUTES)

TIME REQUIRED FOR 5 PERSON4 + 5 * 5 = 29 MINUTES

TIME REQUIRED FOR ALL PERSON29 * 2 = 58 MINUTES

WITH CONCURRENCY

Page 72: Golang 101 (Concurrency vs Parallelism)

1 2 3 4 5 6 7 8 9 10

DOCTOR

IN (4 M

INUTES)

CHECK-UP (5 MINUTES)

TIME REQUIRED FOR ALL PERSON4 + 5 * 5 = 29 MINUTES

WITH CONCURRENCY + PARALLELISM

DOCTOR

CHECK-UP (5 MINUTES)

IN (4 MINUTES)

Page 73: Golang 101 (Concurrency vs Parallelism)
Page 74: Golang 101 (Concurrency vs Parallelism)

Companies currently using Go throughout the world

https://github.com/golang/go/wiki/GoUsers

Page 75: Golang 101 (Concurrency vs Parallelism)

Where to goTour of Go (https://tour.golang.org)

Go by Example (https://gobyexample.com/)

An Introduction to Programming in Go (https://www.golang-book.com/books/intro)

Google it.

Page 76: Golang 101 (Concurrency vs Parallelism)

Thank you!