websummit 2015 - gopher it

72
Gopher It @gautamrege

Upload: gautam-rege

Post on 14-Apr-2017

3.382 views

Category:

Software


2 download

TRANSCRIPT

Page 1: WebSummit 2015 - Gopher it

Gopher It@gautamrege

Page 2: WebSummit 2015 - Gopher it

Gautam Rege

Page 3: WebSummit 2015 - Gopher it

https://flic.kr/p/7bgkPn

Gautam Rege

Page 4: WebSummit 2015 - Gopher it

https://flic.kr/p/7bgkPn

https://flic.kr/p/9HfsBe

Gautam Rege

Page 5: WebSummit 2015 - Gopher it

Rethink Programming

Page 6: WebSummit 2015 - Gopher it

Rethink Programming

Why Go?

• See various features of Go• Go code snippets• Lot’s of gopher cartoons

Page 7: WebSummit 2015 - Gopher it

@joshsoftwareWhere Programming an Art!

Page 8: WebSummit 2015 - Gopher it

Why was another language required?

Arn’t there enough already?

https://flic.kr/p/bk3mFf

Page 9: WebSummit 2015 - Gopher it

Picking a language?Safety Speed

Ease

?Type

Syntax CompilationRuntime

IPC

Page 10: WebSummit 2015 - Gopher it

Picking a language?Safety Speed

Ease

?Type

Syntax

ReadableMaintainable

Learning

CompilationRuntime

IPC

Page 11: WebSummit 2015 - Gopher it

Picking a language?Safety Speed

Ease

?Type

Syntax

ReadableMaintainable

Learning

CompilationRuntime

IPC

Page 12: WebSummit 2015 - Gopher it

Inspiration for Go

https://speakerdeck.com/bg/the-roots-of-go

Page 13: WebSummit 2015 - Gopher it

Go is C on Steroids

Page 14: WebSummit 2015 - Gopher it

Go is C on Steroids

Page 15: WebSummit 2015 - Gopher it

Go is C on Steroids

No more memory leaks

Page 16: WebSummit 2015 - Gopher it

Go is C on Steroids

No more memory leaks

Maps, Slices Closures

Concurrency

Page 17: WebSummit 2015 - Gopher it

Go is C on Steroids

No more memory leaks

Maps, Slices Closures

Concurrency

go fmtgo buildgo getgo fix

Page 18: WebSummit 2015 - Gopher it

Concurrency Parallelism

Multi-core Processing

Concurrency is not Parallelism

Page 19: WebSummit 2015 - Gopher it

Concurrency Independent executing components

Parallelism Execution in parallel

Page 20: WebSummit 2015 - Gopher it

“Don’t communicate by sharing memory.

Share memory by communicating.”

Rob Pike

Page 21: WebSummit 2015 - Gopher it

Concurrency and Parallelism

1 2 3 4

Page 22: WebSummit 2015 - Gopher it

Concurrency and Parallelism

1 2 3 4

Page 23: WebSummit 2015 - Gopher it
Page 24: WebSummit 2015 - Gopher it
Page 25: WebSummit 2015 - Gopher it

CSPCommunicating Sequential Processes

https://flic.kr/p/6MwYFo

Page 26: WebSummit 2015 - Gopher it

CSPCommunicating Sequential Processes

https://flic.kr/p/awu6ZA

since 1978 !!https://flic.kr/p/6MwYFo

Page 27: WebSummit 2015 - Gopher it

Is Golang Object Oriented?

Page 28: WebSummit 2015 - Gopher it

type Attendee struct { Name string Country string phone string ticket string}

type Speaker struct { Attendee slot time.Time}

type Conference struct { Name string Location string people []*Attendee}

Access Specifiers

Page 29: WebSummit 2015 - Gopher it

type Attendee struct { Name string Country string phone string ticket string}

type Speaker struct { Attendee slot time.Time}

type Conference struct { Name string Location string people []*Attendee}

Access Specifiers

Page 30: WebSummit 2015 - Gopher it

Export with Case Sensitiveness Exported / Unexported

“Nothing is really protected”

type Attendee struct { Name string Country string phone string ticket string}

type Speaker struct { Attendee slot time.Time}

type Conference struct { Name string Location string people []*Attendee}

Access Specifiers

Page 31: WebSummit 2015 - Gopher it

Structs, not classes Embedded structs

type Attendee struct { Name string Country string phone string ticket string}

type Speaker struct { Attendee slot time.Time}

type Conference struct { Name string Location string people []*Attendee}

Inheritance

Page 32: WebSummit 2015 - Gopher it

Structs, not classes Embedded structs

type Attendee struct { Name string Country string phone string ticket string}

type Speaker struct { Attendee slot time.Time}

type Conference struct { Name string Location string people []*Attendee}

Inheritance

mm

Page 33: WebSummit 2015 - Gopher it

Structs, not classes Embedded structs

type Attendee struct { Name string Country string phone string ticket string}

type Speaker struct { Attendee slot time.Time}

type Conference struct { Name string Location string people []*Attendee}

Inheritance

mmm

Page 34: WebSummit 2015 - Gopher it

func (c *Conference) BuyTicket() (ticket string, err error) { //payment gateway stuff return "R-00001", nil}

func (person *Attendee) Attend(c *Conference) bool { ticket, err := c.BuyTicket() if err != nil { // handle error }

person.ticket = ticket return true}

Functions with Receivers

Page 35: WebSummit 2015 - Gopher it

func (c *Conference) BuyTicket() (ticket string, err error) { //payment gateway stuff return "R-00001", nil}

func (person *Attendee) Attend(c *Conference) bool { ticket, err := c.BuyTicket() if err != nil { // handle error }

person.ticket = ticket return true}

Functions with Receivers

Page 36: WebSummit 2015 - Gopher it

func (c *Conference) BuyTicket() (ticket string, err error) { //payment gateway stuff return "R-00001", nil}

func (person *Attendee) Attend(c *Conference) bool { ticket, err := c.BuyTicket() if err != nil { // handle error }

person.ticket = ticket return true}

Functions with Receivers

Page 37: WebSummit 2015 - Gopher it

type SocialNetworker interface { OnFacebook() string OnTwitter() string}

InterfacesDon’t implement interfaces.

They happen!

Page 38: WebSummit 2015 - Gopher it

type SocialNetworker interface { OnFacebook() string OnTwitter() string}

Interfaces

func (Attendee) OnFacebook() string { return “my fb account"}func (Attendee) OnTwitter() string { return "my twitter account"}func (Conference) OnFacebook() string { return "conf fb account"}func (Conference) OnTwitter() string { return "conf twitter account"}

Page 39: WebSummit 2015 - Gopher it

Polymorphism

var social SocialNetworker

social = me // Attendeefmt.Println("me: ", social.OnFacebook())

social = c // Conferencefmt.Println("me: ", social.OnFacebook())

Page 40: WebSummit 2015 - Gopher it

Polymorphism

var social SocialNetworker

social = me // Attendeefmt.Println("me: ", social.OnFacebook())

social = c // Conferencefmt.Println("me: ", social.OnFacebook())

Page 41: WebSummit 2015 - Gopher it

Programming Ethics

Page 42: WebSummit 2015 - Gopher it

Programmer Awareness

Page 43: WebSummit 2015 - Gopher it

Programmer Awareness

Page 44: WebSummit 2015 - Gopher it

Programmer Awareness

Page 45: WebSummit 2015 - Gopher it

Programmer Awareness

Page 46: WebSummit 2015 - Gopher it

Programmer Awareness

Page 47: WebSummit 2015 - Gopher it

Programmer Awareness

Page 48: WebSummit 2015 - Gopher it

y := 2 // y is of type inty = "3"

Programming Awareness

Page 49: WebSummit 2015 - Gopher it

y := 2 // y is of type inty = "3"

Programming Awareness

Compile Error Did you just

assign a string to an integer?

Page 50: WebSummit 2015 - Gopher it

Programming Awarenessconfs := make(map[string]Conference)

for key, value := range confs { fmt.Println(value.Name)}

Page 51: WebSummit 2015 - Gopher it

Programming Awarenessconfs := make(map[string]Conference)

for key, value := range confs { fmt.Println(value.Name)}

Just saying - you should not iterate a hash, man!

I’l randomise the order now!

Page 52: WebSummit 2015 - Gopher it

deferfunc trace(s string) { fmt.Println("entering:", s) }func untrace(s string) { fmt.Println("leaving:", s) }

func main() { trace("main") defer untrace("main") // do your thing.}

Page 53: WebSummit 2015 - Gopher it

deferfunc trace(s string) { fmt.Println("entering:", s) }func untrace(s string) { fmt.Println("leaving:", s) }

func main() { trace("main") defer untrace("main") // do your thing.}

func trace(s string) string { fmt.Println("entering:", s) return s}

func main() { defer untrace(trace(“main”)) // do your thing.}

Page 54: WebSummit 2015 - Gopher it

Concurrency and Parallelism

1 2 3 4

Page 55: WebSummit 2015 - Gopher it

Concurrency and Parallelism

1 2 3 4

Goroutines

Page 56: WebSummit 2015 - Gopher it

Concurrency and Parallelism

1 2 3 4

Channels

Page 57: WebSummit 2015 - Gopher it

Ah… Some Go code (finally)package main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

Page 58: WebSummit 2015 - Gopher it

Packagespackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

package main

import ( "fmt" "runtime" "sync")

var wg sync.WaitGroup

Page 59: WebSummit 2015 - Gopher it

Channelspackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait()}

Page 60: WebSummit 2015 - Gopher it

Channelspackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait()}

Page 61: WebSummit 2015 - Gopher it

Go Routinespackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait()}

Page 62: WebSummit 2015 - Gopher it

Writing to a Channelpackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait()}

Page 63: WebSummit 2015 - Gopher it

Goroutines and deferpackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churnfor count := 0; count < 300; count++ {for char := 0; char < 30000; char++ {fmt.Printf("")

}}

fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton)}

Page 64: WebSummit 2015 - Gopher it

Reading from a Channelpackage main

import ( "fmt" "runtime" "sync" )

var wg sync.WaitGroup

/* * Each go-routine sleeps at random(1-5) seconds. * This is simulating long working process * Then we finish in order */ func check_baton(leg int, baton chan int) { for value := range baton { switch value { case leg: // pass the baton fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return default: // ignore baton <- value } } }

func run(leg int, baton chan int) { defer wg.Done() fmt.Printf("Leg %d.. churning\n", leg)

// Massive CPU churn for count := 0; count < 300; count++ { for char := 0; char < 30000; char++ { fmt.Printf("") } } fmt.Printf("Leg %d.. churned, waiting to exit\n", leg)

check_baton(leg, baton) }

func main() { runtime.GOMAXPROCS(4)

baton := make(chan int) wg.Add(4)

for i := 1; i < 5; i++ { go run(i, baton) }

// start the race baton <- 1

// wait for relay to finish wg.Wait() }

func check_baton(leg int, baton chan int) {for value := range baton { switch value { case leg: // pass the baton

fmt.Println("Finished leg ", leg) if leg == 4 { close(baton) } else { baton <- leg + 1 } return

default: // ignore baton <- value

}}

}

Page 65: WebSummit 2015 - Gopher it

Go in Production

Page 66: WebSummit 2015 - Gopher it

Smart City Technology

• Help / Panic / 911

• Complaints

• e-Wallet & e-Shopping

• Hospital Admission

• e-Cycle Management

• Township Surveillance

• Visitor Management

Utility Metering & Billing• Water

• Electricity

• Gas

• Smart Distribution Box

Communication• Internet

• DTH

• Telephony

• Video On Demand

• e-Learning

• Parking Management

• Bank Auto-debit

• Township Smart Debit card

• Hospital Admission

• e-Cycle Management

• Digital Door Locks

• Asset Tag Tracking

• Smart Street Lighting

Services

Security

Page 67: WebSummit 2015 - Gopher it

Building Smart Cities

Page 68: WebSummit 2015 - Gopher it

Building Smart Circuitsmodbus

Ethernetmbuswmbuszigbee

Page 69: WebSummit 2015 - Gopher it
Page 70: WebSummit 2015 - Gopher it

Adoption of Go

Page 71: WebSummit 2015 - Gopher it

Golang Referenceshttp://golang.org

https://tour.golang.org

https://golang.org/doc/effective_go.html

https://groups.google.com/group/golang-nuts

https://golang.org/play

http://blog.gopheracademy.com

http://www.goinggo.net

http://golang-challenge.com

Page 72: WebSummit 2015 - Gopher it

Let the games begin !

@gautamrege

Gophers

@joshsoftware