Post on 05-Apr-2017
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 Ayunda22
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.
Go keywordsbreak default func interface selectcase defer go map structchan else goto package switchconst fallthrough if range typecontinue for import return var
Yea I’m ready to learn this language
Basic structure1.package main
2.
3.import “fmt”
4.
5.func main() {
6. fmt.Printf(“Hello, world.\n”)
7.}
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.}
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
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
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
Short declaration variables1.package main
2. …
3.
4.func main() {
5. y := “Hi!”
6. fmt.Println(y)
7.}
OutputHi!
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
Looping1.package main
2. …
3.
4.func main() {
5. for i := 1; i <= 10; i++ {
6. fmt.Println(i)
7. }
8.}
Output12345678910
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
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
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}
Array, slice, map, methods, pointer, interfaces etc.
https://tour.golang.org/list
What the heck is concurrency
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
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 PERSON4 + 5 = 9 MINUTES
TIME REQUIRED FOR ALL PERSON9 * 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 PERSON4 + 5 * 5 = 29 MINUTES
TIME REQUIRED FOR ALL PERSON29 * 2 = 58 MINUTES
WITH CONCURRENCY
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)
Companies currently using Go throughout the world
https://github.com/golang/go/wiki/GoUsers
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.
Thank you!