websummit 2015 - gopher it

Post on 14-Apr-2017

3.382 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Gopher It@gautamrege

Gautam Rege

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

Gautam Rege

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

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

Gautam Rege

Rethink Programming

Rethink Programming

Why Go?

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

@joshsoftwareWhere Programming an Art!

Why was another language required?

Arn’t there enough already?

https://flic.kr/p/bk3mFf

Picking a language?Safety Speed

Ease

?Type

Syntax CompilationRuntime

IPC

Picking a language?Safety Speed

Ease

?Type

Syntax

ReadableMaintainable

Learning

CompilationRuntime

IPC

Picking a language?Safety Speed

Ease

?Type

Syntax

ReadableMaintainable

Learning

CompilationRuntime

IPC

Inspiration for Go

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

Go is C on Steroids

Go is C on Steroids

Go is C on Steroids

No more memory leaks

Go is C on Steroids

No more memory leaks

Maps, Slices Closures

Concurrency

Go is C on Steroids

No more memory leaks

Maps, Slices Closures

Concurrency

go fmtgo buildgo getgo fix

Concurrency Parallelism

Multi-core Processing

Concurrency is not Parallelism

Concurrency Independent executing components

Parallelism Execution in parallel

“Don’t communicate by sharing memory.

Share memory by communicating.”

Rob Pike

Concurrency and Parallelism

1 2 3 4

Concurrency and Parallelism

1 2 3 4

CSPCommunicating Sequential Processes

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

CSPCommunicating Sequential Processes

https://flic.kr/p/awu6ZA

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

Is Golang Object Oriented?

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

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

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

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

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

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

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

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

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

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

InterfacesDon’t implement interfaces.

They happen!

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"}

Polymorphism

var social SocialNetworker

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

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

Polymorphism

var social SocialNetworker

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

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

Programming Ethics

Programmer Awareness

Programmer Awareness

Programmer Awareness

Programmer Awareness

Programmer Awareness

Programmer Awareness

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

Programming Awareness

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

Programming Awareness

Compile Error Did you just

assign a string to an integer?

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

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

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!

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.}

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.}

Concurrency and Parallelism

1 2 3 4

Concurrency and Parallelism

1 2 3 4

Goroutines

Concurrency and Parallelism

1 2 3 4

Channels

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() }

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

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()}

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()}

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()}

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()}

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)}

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

}}

}

Go in Production

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

Building Smart Cities

Building Smart Circuitsmodbus

Ethernetmbuswmbuszigbee

Adoption of Go

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

Let the games begin !

@gautamrege

Gophers

@joshsoftware

top related