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 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
31. 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
32. 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
33. 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
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!
57. 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()
}
58. 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
59. 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()
}
60. 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()
}
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 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()
}
63. 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)
}
64. 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
}
}
}