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
@joshsoftware
Where 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 Compilation
Runtime
IPC
Picking a language?
Safety Speed
Ease
?
Type
Syntax
Readable
Maintainable
Learning
Compilation
Runtime
IPC
Picking a language?
Safety Speed
Ease
?
Type
Syntax
Readable
Maintainable
Learning
Compilation
Runtime
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 fmt
go build
go get
go 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
CSP
Communicating Sequential Processes
https://flic.kr/p/6MwYFo
CSP
Communicating 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
m
m
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
m
m
m
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
}
Interfaces
Don’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 // Attendee
fmt.Println("me: ", social.OnFacebook())
social = c // Conference
fmt.Println("me: ", social.OnFacebook())
Polymorphism
var social SocialNetworker
social = me // Attendee
fmt.Println("me: ", social.OnFacebook())
social = c // Conference
fmt.Println("me: ", social.OnFacebook())
Programming Ethics
Programmer Awareness
Programmer Awareness
Programmer Awareness
Programmer Awareness
Programmer Awareness
Programmer Awareness
y := 2 // y is of type int
y = "3"
Programming Awareness
y := 2 // y is of type int
y = "3"
Programming Awareness
Compile Error
Did you just
assign a string
to an integer?
Programming Awareness
confs := make(map[string]Conference)
for key, value := range confs {
fmt.Println(value.Name)
}
Programming Awareness
confs := 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!
defer
func 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.
}
defer
func 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.. churningn", 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 exitn", 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()
}
Packages
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.. churningn", 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 exitn", 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
Channels
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.. churningn", 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 exitn", 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()
}
Channels
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.. churningn", 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 exitn", 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 Routines
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.. churningn", 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 exitn", 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 Channel
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.. churningn", 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 exitn", 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 defer
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.. churningn", 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 exitn", 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.. churningn", 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)
}
Reading from a Channel
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.. churningn", 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 exitn", 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 Circuits
modbus
Ethernetmbus
wmbus
zigbee
Adoption of Go
Golang References
http://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

WebSummit 2015 - Gopher it

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
    Rethink Programming Why Go? •See various features of Go • Go code snippets • Lot’s of gopher cartoons
  • 7.
  • 8.
    Why was another languagerequired? Arn’t there enough already? https://flic.kr/p/bk3mFf
  • 9.
    Picking a language? SafetySpeed Ease ? Type Syntax Compilation Runtime IPC
  • 10.
    Picking a language? SafetySpeed Ease ? Type Syntax Readable Maintainable Learning Compilation Runtime IPC
  • 11.
    Picking a language? SafetySpeed Ease ? Type Syntax Readable Maintainable Learning Compilation Runtime IPC
  • 12.
  • 13.
    Go is Con Steroids
  • 14.
    Go is Con Steroids
  • 15.
    Go is Con Steroids No more memory leaks
  • 16.
    Go is Con Steroids No more memory leaks Maps, Slices Closures Concurrency
  • 17.
    Go is Con Steroids No more memory leaks Maps, Slices Closures Concurrency go fmt go build go get go fix
  • 18.
  • 19.
  • 20.
    “Don’t communicate by sharingmemory. Share memory by communicating.” Rob Pike
  • 21.
  • 22.
  • 25.
  • 26.
  • 27.
  • 28.
    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
  • 29.
    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
  • 30.
    Export with CaseSensitiveness 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
  • 31.
    Structs, not classes Embeddedstructs 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
  • 32.
    Structs, not classes Embeddedstructs 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 m m
  • 33.
    Structs, not classes Embeddedstructs 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 m m m
  • 34.
    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
  • 35.
    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
  • 36.
    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
  • 37.
    type SocialNetworker interface{ OnFacebook() string OnTwitter() string } Interfaces Don’t implement interfaces. They happen!
  • 38.
    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" }
  • 39.
    Polymorphism var social SocialNetworker social= me // Attendee fmt.Println("me: ", social.OnFacebook()) social = c // Conference fmt.Println("me: ", social.OnFacebook())
  • 40.
    Polymorphism var social SocialNetworker social= me // Attendee fmt.Println("me: ", social.OnFacebook()) social = c // Conference fmt.Println("me: ", social.OnFacebook())
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
    y := 2// y is of type int y = "3" Programming Awareness
  • 49.
    y := 2// y is of type int y = "3" Programming Awareness Compile Error Did you just assign a string to an integer?
  • 50.
    Programming Awareness confs :=make(map[string]Conference) for key, value := range confs { fmt.Println(value.Name) }
  • 51.
    Programming Awareness confs :=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!
  • 52.
    defer func 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. }
  • 53.
    defer func 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. }
  • 54.
  • 55.
  • 56.
  • 57.
    Ah… Some Gocode (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.. churningn", 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 exitn", 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() }
  • 58.
    Packages package main import ( "fmt" "runtime" "sync" ) varwg 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.. churningn", 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 exitn", 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
  • 59.
    Channels package main import ( "fmt" "runtime" "sync" ) varwg 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.. churningn", 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 exitn", 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() }
  • 60.
    Channels package main import ( "fmt" "runtime" "sync" ) varwg 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.. churningn", 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 exitn", 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() }
  • 61.
    Go Routines 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.. churningn", 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 exitn", 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() }
  • 62.
    Writing to aChannel 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.. churningn", 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 exitn", 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() }
  • 63.
    Goroutines and defer packagemain 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.. churningn", 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 exitn", 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.. churningn", 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) }
  • 64.
    Reading from aChannel 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.. churningn", 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 exitn", 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 } } }
  • 65.
  • 66.
    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
  • 67.
  • 68.
  • 70.
  • 71.
  • 72.
    Let the gamesbegin ! @gautamrege Gophers @joshsoftware