KEMBAR78
Golang 101 (Concurrency vs Parallelism) | PPTX
fmt.Printf(“Hello, UNSADA”)
Pramesti Hatta K.
Software Engineer @Tech in Asia
Making things look hard is easy. Making hard
things look easy, that’s hard.
Meet Gopher, the funniest programming
language mascot.
2 types of programming language
compiled | interpreted
compiled languages are converted directly into
machine code
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.
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
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 Ayunda
22
New programming language?
2007
Robert Griesemer, Rob Pike and Ken
Thompson
2009 (became open source)
2012 (finally stable v.)
why?
Go was born out of frustration with existing
languages and environments for systems
programming.
one had to choose either efficient compilation,
efficient execution, or ease of programming
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.
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.
designed by Google to solve Google’s
problem.
and Google has big problems.
Big hardware
Big software
● C++ (mostly) for servers, plus lots of java
and python
● thousands of engineers
● gazillions of lines of code
development at Google can be slow,
often clumsy.
Goals
● eliminate slowness
● eliminate clumsiness
● improve effectiveness
Go’s purpose is not research into
programming language design
Go’s purpose is to make software
engineer’s lives better.
Go Programming Language
● open source
● concurrent
● garbage collected
● efficient
● simple
● fun
● boring (to some)
https://golang.org
Go is a statically typed compiled
language.
Golang is a simple language. It has only 25 keywords.
Go keywords
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Yea I’m ready to learn this language
Basic structure
1.package main
2.
3.import “fmt”
4.
5.func main() {
6. fmt.Printf(“Hello, world.n”)
7.}
Package
1.package main
2.
3.import (
4. “fmt”
5. “math/rand”
6.)
7.
8.func main() {
Function
1.package main
2. …
3.func add(x int, y int) int
{
4. return x + y
5.}
6.
7.func main() {
Output
30
Function with multiple return
1.package main
2. …
3.func swap(x, y string) (string, string)
{
4. return y, x
5.}
6.
7.func main() {
Output
world hello
Variables
1.package main
2. …
3.var x int
4.
5.func main() {
6. var y string
7. y = "Hi!"
8. fmt.Println(y)
Output
Hi!
0
Short declaration variables
1.package main
2. …
3.
4.func main() {
5. y := “Hi!”
6. fmt.Println(y)
7.}
Output
Hi!
Basic types
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
float32 float64
complex64 complex128
Looping
1.package main
2. …
3.
4.func main() {
5. for i := 1; i <= 10; i++ {
6. fmt.Println(i)
7. }
Output
1
2
3
4
5
6
7
8
9
10
Looping continued
1.package main
2. …
3.
4.func main() {
5. i := 1
6. for i <= 10 {
7. fmt.Println(i)
8. i++
Output
1
2
3
4
5
6
7
8
9
10
If and else
1.package main
2. …
3.
4.func main() {
5. i := 22
6. if i > 10 {
7. fmt.Println(“big”)
8. } else {
Output
big
Struct
1.package main
2. …
3.
4.type PersegiPanjang struct {
5. P, L int
6.}
7.func main() {
Output
{10 22}
Array, slice, map, methods, pointer,
interfaces etc.
https://tour.golang.org/list
What the heck is concurrency
concurrency
concurrency
People will tend to say ...
concurrency is doing two or more things
simultaneously,
parallelism is doing two or more things
simultaneously.
concurrency is not parallelism
concurrency
“Gue lagi ngobrol
sambil ngopi”
parallelism
“Gue lagi ngoding
sambil denger musik”
concurrency is the composition of
independently executing processes
parallelism is the simultaneous
execution of (possibly related)
computations
concurrency is about dealing with lots
of things at once. parallelism is about
doing lots of things at once
concurrency is about the structure.
while parallelism is about the
execution.
but, concurrency can enable parallelism
how?
to have true parallelism you need to
run your program on a machine with
multiple physical processors
You need analogies
Imagine you have a restaurant
you have no employee
You only have one stove
there is one customer
she orders “omelette” and “french fries”
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
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
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
1 2 3 4 5 6 7 8 9 10
DOCTOR
IN (4 MINUTES)
CHECK-UP (5 MINUTES)
TIME REQUIRED FOR 1 PERSON
4 + 5 = 9 MINUTES
TIME REQUIRED FOR ALL PERSON
9 * 10 = 90 MINUTES
WITHOUT
CONCURRENCY
1 2 3 4 5 6 7 8 9 10
DOCTOR
IN (4 MINUTES)
CHECK-UP (5 MINUTES)
TIME REQUIRED FOR 5 PERSON
4 + 5 * 5 = 29 MINUTES
TIME REQUIRED FOR ALL PERSON
29 * 2 = 58 MINUTES
WITH
CONCURRENCY
1 2 3 4 5 6 7 8 9 10
DOCTOR
CHECK-UP (5 MINUTES)
TIME REQUIRED FOR ALL PERSON
4 + 5 * 5 = 29 MINUTES
WITH
CONCURRENCY +
PARALLELISM
DOCTOR
CHECK-UP (5 MINUTES)
Companies currently using Go throughout the world
https://github.com/golang/go/wiki/GoUsers
Where to go
Tour 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.
Thank you!

Golang 101 (Concurrency vs Parallelism)

  • 1.
  • 2.
    Pramesti Hatta K. SoftwareEngineer @Tech in Asia
  • 3.
    Making things lookhard is easy. Making hard things look easy, that’s hard.
  • 5.
    Meet Gopher, thefunniest programming language mascot.
  • 7.
    2 types ofprogramming language compiled | interpreted
  • 8.
    compiled languages areconverted directly into machine code
  • 9.
    In contrast tocompiled 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.
  • 11.
    compiled ● often fasterexecution time ● often slower development time C/C++, Go, Fortran, Pascal etc interpreted ● often slower execution time ● often faster development time PHP, Python, Ruby, JavaScript
  • 12.
    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 Ayunda 22
  • 14.
  • 15.
  • 16.
    Robert Griesemer, RobPike and Ken Thompson
  • 17.
  • 18.
  • 19.
  • 20.
    Go was bornout of frustration with existing languages and environments for systems programming.
  • 21.
    one had tochoose either efficient compilation, efficient execution, or ease of programming
  • 22.
    programmers who couldwere 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.
  • 23.
    Go is anattempt to combine the ease of programming of an interpreted, dynamically typed language with the efficiency and safety of a statically typed, compiled language.
  • 24.
    designed by Googleto solve Google’s problem.
  • 25.
    and Google hasbig problems.
  • 26.
  • 27.
    Big software ● C++(mostly) for servers, plus lots of java and python ● thousands of engineers ● gazillions of lines of code
  • 28.
    development at Googlecan be slow, often clumsy.
  • 29.
    Goals ● eliminate slowness ●eliminate clumsiness ● improve effectiveness
  • 30.
    Go’s purpose isnot research into programming language design
  • 31.
    Go’s purpose isto make software engineer’s lives better.
  • 32.
    Go Programming Language ●open source ● concurrent ● garbage collected ● efficient ● simple ● fun ● boring (to some) https://golang.org
  • 33.
    Go is astatically typed compiled language.
  • 34.
    Golang is asimple language. It has only 25 keywords.
  • 36.
    Go keywords break defaultfunc interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var
  • 38.
    Yea I’m readyto learn this language
  • 39.
    Basic structure 1.package main 2. 3.import“fmt” 4. 5.func main() { 6. fmt.Printf(“Hello, world.n”) 7.}
  • 40.
    Package 1.package main 2. 3.import ( 4.“fmt” 5. “math/rand” 6.) 7. 8.func main() {
  • 41.
    Function 1.package main 2. … 3.funcadd(x int, y int) int { 4. return x + y 5.} 6. 7.func main() { Output 30
  • 42.
    Function with multiplereturn 1.package main 2. … 3.func swap(x, y string) (string, string) { 4. return y, x 5.} 6. 7.func main() { Output world hello
  • 43.
    Variables 1.package main 2. … 3.varx int 4. 5.func main() { 6. var y string 7. y = "Hi!" 8. fmt.Println(y) Output Hi! 0
  • 44.
    Short declaration variables 1.packagemain 2. … 3. 4.func main() { 5. y := “Hi!” 6. fmt.Println(y) 7.} Output Hi!
  • 45.
    Basic types bool string int int8int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte // alias for uint8 rune // alias for int32 float32 float64 complex64 complex128
  • 46.
    Looping 1.package main 2. … 3. 4.funcmain() { 5. for i := 1; i <= 10; i++ { 6. fmt.Println(i) 7. } Output 1 2 3 4 5 6 7 8 9 10
  • 47.
    Looping continued 1.package main 2.… 3. 4.func main() { 5. i := 1 6. for i <= 10 { 7. fmt.Println(i) 8. i++ Output 1 2 3 4 5 6 7 8 9 10
  • 48.
    If and else 1.packagemain 2. … 3. 4.func main() { 5. i := 22 6. if i > 10 { 7. fmt.Println(“big”) 8. } else { Output big
  • 49.
    Struct 1.package main 2. … 3. 4.typePersegiPanjang struct { 5. P, L int 6.} 7.func main() { Output {10 22}
  • 50.
    Array, slice, map,methods, pointer, interfaces etc. https://tour.golang.org/list
  • 52.
    What the heckis concurrency
  • 53.
  • 54.
    People will tendto say ... concurrency is doing two or more things simultaneously, parallelism is doing two or more things simultaneously.
  • 55.
    concurrency is notparallelism
  • 56.
    concurrency “Gue lagi ngobrol sambilngopi” parallelism “Gue lagi ngoding sambil denger musik”
  • 57.
    concurrency is thecomposition of independently executing processes
  • 58.
    parallelism is thesimultaneous execution of (possibly related) computations
  • 59.
    concurrency is aboutdealing with lots of things at once. parallelism is about doing lots of things at once
  • 60.
    concurrency is aboutthe structure. while parallelism is about the execution.
  • 61.
    but, concurrency canenable parallelism
  • 62.
  • 63.
    to have trueparallelism you need to run your program on a machine with multiple physical processors
  • 64.
  • 65.
    Imagine you havea restaurant you have no employee You only have one stove there is one customer she orders “omelette” and “french fries”
  • 66.
    Without concurrency 1.you goto 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
  • 67.
    With concurrency 1.you goto 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
  • 68.
    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
  • 70.
    1 2 34 5 6 7 8 9 10 DOCTOR IN (4 MINUTES) CHECK-UP (5 MINUTES) TIME REQUIRED FOR 1 PERSON 4 + 5 = 9 MINUTES TIME REQUIRED FOR ALL PERSON 9 * 10 = 90 MINUTES WITHOUT CONCURRENCY
  • 71.
    1 2 34 5 6 7 8 9 10 DOCTOR IN (4 MINUTES) CHECK-UP (5 MINUTES) TIME REQUIRED FOR 5 PERSON 4 + 5 * 5 = 29 MINUTES TIME REQUIRED FOR ALL PERSON 29 * 2 = 58 MINUTES WITH CONCURRENCY
  • 72.
    1 2 34 5 6 7 8 9 10 DOCTOR CHECK-UP (5 MINUTES) TIME REQUIRED FOR ALL PERSON 4 + 5 * 5 = 29 MINUTES WITH CONCURRENCY + PARALLELISM DOCTOR CHECK-UP (5 MINUTES)
  • 74.
    Companies currently usingGo throughout the world https://github.com/golang/go/wiki/GoUsers
  • 75.
    Where to go Tourof 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.
  • 76.

Editor's Notes