WebSummit 2015 - Gopher it
-
Upload
gautam-rege -
Category
Software
-
view
3.382 -
download
2
Transcript of WebSummit 2015 - Gopher it
![Page 1: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/1.jpg)
Gopher It@gautamrege
![Page 2: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/2.jpg)
Gautam Rege
![Page 3: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/3.jpg)
https://flic.kr/p/7bgkPn
Gautam Rege
![Page 4: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/4.jpg)
https://flic.kr/p/7bgkPn
https://flic.kr/p/9HfsBe
Gautam Rege
![Page 5: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/5.jpg)
Rethink Programming
![Page 6: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/6.jpg)
Rethink Programming
Why Go?
• See various features of Go• Go code snippets• Lot’s of gopher cartoons
![Page 7: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/7.jpg)
@joshsoftwareWhere Programming an Art!
![Page 8: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/8.jpg)
Why was another language required?
Arn’t there enough already?
https://flic.kr/p/bk3mFf
![Page 9: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/9.jpg)
Picking a language?Safety Speed
Ease
?Type
Syntax CompilationRuntime
IPC
![Page 10: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/10.jpg)
Picking a language?Safety Speed
Ease
?Type
Syntax
ReadableMaintainable
Learning
CompilationRuntime
IPC
![Page 11: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/11.jpg)
Picking a language?Safety Speed
Ease
?Type
Syntax
ReadableMaintainable
Learning
CompilationRuntime
IPC
![Page 12: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/12.jpg)
Inspiration for Go
https://speakerdeck.com/bg/the-roots-of-go
![Page 13: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/13.jpg)
Go is C on Steroids
![Page 14: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/14.jpg)
Go is C on Steroids
![Page 15: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/15.jpg)
Go is C on Steroids
No more memory leaks
![Page 16: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/16.jpg)
Go is C on Steroids
No more memory leaks
Maps, Slices Closures
Concurrency
![Page 17: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/17.jpg)
Go is C on Steroids
No more memory leaks
Maps, Slices Closures
Concurrency
go fmtgo buildgo getgo fix
![Page 18: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/18.jpg)
Concurrency Parallelism
Multi-core Processing
Concurrency is not Parallelism
![Page 19: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/19.jpg)
Concurrency Independent executing components
Parallelism Execution in parallel
![Page 20: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/20.jpg)
“Don’t communicate by sharing memory.
Share memory by communicating.”
Rob Pike
![Page 21: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/21.jpg)
Concurrency and Parallelism
1 2 3 4
![Page 22: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/22.jpg)
Concurrency and Parallelism
1 2 3 4
![Page 23: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/23.jpg)
![Page 24: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/24.jpg)
![Page 25: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/25.jpg)
CSPCommunicating Sequential Processes
https://flic.kr/p/6MwYFo
![Page 26: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/26.jpg)
CSPCommunicating Sequential Processes
https://flic.kr/p/awu6ZA
since 1978 !!https://flic.kr/p/6MwYFo
![Page 27: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/27.jpg)
Is Golang Object Oriented?
![Page 28: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/28.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/29.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/30.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/31.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/32.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/33.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/34.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/35.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/36.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/37.jpg)
type SocialNetworker interface { OnFacebook() string OnTwitter() string}
InterfacesDon’t implement interfaces.
They happen!
![Page 38: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/38.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/39.jpg)
Polymorphism
var social SocialNetworker
social = me // Attendeefmt.Println("me: ", social.OnFacebook())
social = c // Conferencefmt.Println("me: ", social.OnFacebook())
![Page 40: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/40.jpg)
Polymorphism
var social SocialNetworker
social = me // Attendeefmt.Println("me: ", social.OnFacebook())
social = c // Conferencefmt.Println("me: ", social.OnFacebook())
![Page 41: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/41.jpg)
Programming Ethics
![Page 42: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/42.jpg)
Programmer Awareness
![Page 43: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/43.jpg)
Programmer Awareness
![Page 44: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/44.jpg)
Programmer Awareness
![Page 45: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/45.jpg)
Programmer Awareness
![Page 46: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/46.jpg)
Programmer Awareness
![Page 47: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/47.jpg)
Programmer Awareness
![Page 48: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/48.jpg)
y := 2 // y is of type inty = "3"
Programming Awareness
![Page 49: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/49.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/50.jpg)
Programming Awarenessconfs := make(map[string]Conference)
for key, value := range confs { fmt.Println(value.Name)}
![Page 51: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/51.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/52.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/53.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/54.jpg)
Concurrency and Parallelism
1 2 3 4
![Page 55: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/55.jpg)
Concurrency and Parallelism
1 2 3 4
Goroutines
![Page 56: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/56.jpg)
Concurrency and Parallelism
1 2 3 4
Channels
![Page 57: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/57.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/58.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/59.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/60.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/61.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/62.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/63.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/64.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/65.jpg)
Go in Production
![Page 66: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/66.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/67.jpg)
Building Smart Cities
![Page 68: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/68.jpg)
Building Smart Circuitsmodbus
Ethernetmbuswmbuszigbee
![Page 69: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/69.jpg)
![Page 70: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/70.jpg)
Adoption of Go
![Page 71: WebSummit 2015 - Gopher it](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/71.jpg)
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](https://reader031.fdocuments.us/reader031/viewer/2022022413/58efc2f91a28abd22f8b4589/html5/thumbnails/72.jpg)
Let the games begin !
@gautamrege
Gophers
@joshsoftware