SlideShare a Scribd company logo
going loopy
adventures in iteration with google go
@feyeleanor
Going Loopy http://slideshare.net/feyeleanor/1
the task
Going Loopy http://slides.games-with-brains.net/2
0: 0
1: 2
2: 4
3: 6
4: 8
Going Loopy http://slides.games-with-brains.net/3
the conditional loop
Going Loopy http://slides.games-with-brains.net/4
package main
import "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
fmt.Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/5
package main
import "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
fmt.Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/6
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
7Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
8Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
9Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/10
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/11
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/12
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/13
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/14
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/15
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/16
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
17Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/18
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/19
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/20
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i := 0; i < len(s); i++ {
Printf("%v: %vn", i, s[i])
}
}
Going Loopy http://slides.games-with-brains.net/21
the infinite loop
Going Loopy http://slides.games-with-brains.net/22
package main
import . "fmt"
func main() {
defer func() {
recover()
}()
s := []int{0, 2, 4, 6, 8}
i := 0
for {
Printf("%v: %vn", i, s[i])
i++
}
}
Going Loopy http://slides.games-with-brains.net/23
package main
import . "fmt"
func main() {
defer func() {
recover()
}()
s := []int{0, 2, 4, 6, 8}
i := 0
for {
Printf("%v: %vn", i, s[i])
i++
}
}
Going Loopy http://slides.games-with-brains.net/24
package main
import . "fmt"
func main() {
defer func() {
recover()
}()
s := []int{0, 2, 4, 6, 8}
i := 0
for {
Printf("%v: %vn", i, s[i])
i++
}
}
Going Loopy http://slides.games-with-brains.net/25
package main
import . "fmt"
func main() {
defer func() {
recover()
}()
s := []int{0, 2, 4, 6, 8}
i := 0
for {
Printf("%v: %vn", i, s[i])
i++
}
}
Going Loopy http://slides.games-with-brains.net/26
package main
import . "fmt"
func main() {
defer func() {
recover()
}()
s := []int{0, 2, 4, 6, 8}
i := 0
for {
Printf("%v: %vn", i, s[i])
i++
}
}
Going Loopy http://slides.games-with-brains.net/27
package main
import . "fmt"
func main() {
defer func() {
recover()
}()
s := []int{0, 2, 4, 6, 8}
i := 0
for i := 0; ; i++ {
Printf("%v: %vn", i, s[i])
i++
}
}
Going Loopy http://slides.games-with-brains.net/28
the range
Going Loopy http://slides.games-with-brains.net/29
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/30
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/31
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/32
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/33
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/34
a functional interlude
35Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s []int) {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
36Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s []int) {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
37Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s []int) {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
38Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
print_slice(0, 2, 4, 6, 8)
}
func print_slice(s ...int) {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
39Going Loopy http://slides.games-with-brains.net/
asserting type
Going Loopy http://slides.games-with-brains.net/40
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
for i, v := range s.([]int) {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/41
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
for i, v := range s.([]int) {
Printf("%v: %vn", i, v)
}
}
Going Loopy http://slides.games-with-brains.net/42
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
if s, ok := s.([]int); ok {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/43
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
if s, ok := s.([]int); ok {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/44
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
if s, ok := s.([]int); ok {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/45
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
if s, ok := s.([]int); ok {
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/46
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
switch s := s.(type) {
case []int:
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/47
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
switch s := s.(type) {
case []int:
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/48
package main
import . "fmt"
func main() {
print_slice([]int{0, 2, 4, 6, 8})
}
func print_slice(s interface{}) {
switch s := s.(type) {
case []int:
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
Going Loopy http://slides.games-with-brains.net/49
closures
50Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
51Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
52Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
53Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
54Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
55Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
56Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_slice(func(i int) int { return s[i] })
}
func print_slice(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
}
}
57Going Loopy http://slides.games-with-brains.net/
channels
58Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
59Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int, 16)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
60Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
61Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
62Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
63Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
64Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
65Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
66Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
c := make(chan int)
go func() {
for _, v := range []int{0, 2, 4, 6, 8} {
c <- v
}
close(c)
}()
Printf("elements: %vn", print_channel(c))
}
func print_channel(c chan int) (i int) {
for v := range c {
Printf("%v: %vn", i, v)
i++
}
return
}
67Going Loopy http://slides.games-with-brains.net/
upon reflection
68Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
case []int:
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
69Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
case []int:
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
70Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := s.(type) {
case func(int) int:
for i := 0; i < 5; i++ {
Printf("%v: %vn", i, s(i))
}
case []int:
for i, v := range s {
Printf("%v: %vn", i, v)
}
}
}
71Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := reflect.ValueOf(s); s.Kind() {
case reflect.Func:
for i := 0; i < 5; i++ {
p := []reflect.Value{ reflect.ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface{})
}
case reflect.Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i))
}
}
}
72Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := reflect.ValueOf(s); s.Kind() {
case reflect.Func:
for i := 0; i < 5; i++ {
p := []reflect.Value{ reflect.ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case reflect.Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
73Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := reflect.ValueOf(s); s.Kind() {
case reflect.Func:
for i := 0; i < 5; i++ {
p := []reflect.Value{ reflect.ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case reflect.Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
74Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := reflect.ValueOf(s); s.Kind() {
case reflect.Func:
for i := 0; i < 5; i++ {
p := []reflect.Value{ reflect.ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case reflect.Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
75Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := reflect.ValueOf(s); s.Kind() {
case reflect.Func:
for i := 0; i < 5; i++ {
p := []reflect.Value{ reflect.ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case reflect.Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
76Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import . "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := ValueOf(s); s.Kind() {
case Func:
for i := 0; i < 5; i++ {
p := []Value{ ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
77Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import . "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := ValueOf(s); s.Kind() {
case Func:
for i := 0; i < 5; i++ {
p := []Value{ ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
78Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import . "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := ValueOf(s); s.Kind() {
case Func:
for i := 0; i < 5; i++ {
p := []Value{ ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
79Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import . "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := ValueOf(s); s.Kind() {
case Func:
for i := 0; i < 5; i++ {
p := []Value{ ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
80Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
import . "reflect"
func main() {
s := []int{0, 2, 4, 6, 8}
print_values(s)
print_values(func(i int) int { return s[i] })
}
func print_values(s interface{}) {
switch s := ValueOf(s); s.Kind() {
case Func:
for i := 0; i < 5; i++ {
p := []Value{ ValueOf(i) }
Printf("%v: %vn", i, s.Call(p)[0].Interface())
}
case Slice:
for i := 0; i < s.Len(); i++ {
Printf("%v: %vn", i, s.Index(i).Interface())
}
}
}
81Going Loopy http://slides.games-with-brains.net/
user-defined type
82Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
83Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
84Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
85Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
86Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
87Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
88Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
s := IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
89Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type Iterable interface {
Each(func(interface{}))
}
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
90Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type Iterable interface {
Each(func(interface{}))
}
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
91Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type Iterable interface {
Each(func(interface{}))
}
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
92Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type Iterable interface {
Each(func(interface{}))
}
type IterableSlice []int
func (i IterableSlice) Each(f func(interface{})) {
for _, v := range i {
f(v)
}
}
func main() {
var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 }
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
93Going Loopy http://slides.games-with-brains.net/
package main
import . "fmt"
type Iterable interface {
Each(func(interface{}))
}
type IterableLimit int
func (i IterableLimit) Each(f func(interface{})) {
for v := 0; v < i; v++ {
f(v)
}
}
func main() {
var s Iterable = IterableLimit(5)
i := 0
s.Each(func(v interface{}) {
Printf("%v: %vn", i, v)
i++
})
}
94Going Loopy http://slides.games-with-brains.net/
AND YES, I DO
HAVE A BOOK
IN THE WORKS
IF THAT'S
YOUR THING
leanpub://GoNotebook

More Related Content

KEY
Going Loopy
PDF
Going Loopy: Adventures in Iteration with Go
PDF
Go for the paranoid network programmer, 2nd edition
PDF
Implementing Virtual Machines in Go & C
PDF
Hello Go
PDF
10〜30分で何となく分かるGo
PDF
Palestra sobre Collections com Python
PDF
Go vs C++ - CppRussia 2019 Piter BoF
Going Loopy
Going Loopy: Adventures in Iteration with Go
Go for the paranoid network programmer, 2nd edition
Implementing Virtual Machines in Go & C
Hello Go
10〜30分で何となく分かるGo
Palestra sobre Collections com Python
Go vs C++ - CppRussia 2019 Piter BoF

What's hot (20)

PDF
Clustering com numpy e cython
PDF
Testing CLI tools with Go
PDF
Go for the paranoid network programmer
KEY
PDF
Python Peculiarities
PDF
Python于Web 2.0网站的应用 - QCon Beijing 2010
PDF
Go for the would be network programmer
PDF
Call Return Exploration
PDF
The Ring programming language version 1.4 book - Part 15 of 30
PDF
EUnit in Practice(Japanese)
PDF
Calling Functions
PDF
Python for High School Programmers
PDF
EuroPython 2015 - Decorators demystified
PDF
Meck at erlang factory, london 2011
PDF
FPBrno 2018-05-22: Benchmarking in elixir
PDF
The Browser Environment - A Systems Programmer's Perspective
PDF
Bai Giang 11
PPTX
Simple swing programs
DOCX
Application Form
PDF
Go for the paranoid network programmer, 3rd edition
Clustering com numpy e cython
Testing CLI tools with Go
Go for the paranoid network programmer
Python Peculiarities
Python于Web 2.0网站的应用 - QCon Beijing 2010
Go for the would be network programmer
Call Return Exploration
The Ring programming language version 1.4 book - Part 15 of 30
EUnit in Practice(Japanese)
Calling Functions
Python for High School Programmers
EuroPython 2015 - Decorators demystified
Meck at erlang factory, london 2011
FPBrno 2018-05-22: Benchmarking in elixir
The Browser Environment - A Systems Programmer's Perspective
Bai Giang 11
Simple swing programs
Application Form
Go for the paranoid network programmer, 3rd edition
Ad

Similar to Going Loopy - Adventures in Iteration with Google Go (20)

PDF
going loopy - adventures in iteration with google go
PDF
going loopy - adventures in iteration with go - Eleanor McHugh - Codemotion M...
PDF
An introduction to functional programming with go
KEY
GoLightly: Building VM-Based Language Runtimes with Google Go
PDF
GoLightly - a customisable virtual machine written in Go
PDF
Go: It's Not Just For Google
KEY
Playing Go with Clojure
PDF
Implementing Software Machines in C and Go
TXT
C++ programming exercise
PDF
Description For this part of the assignment, you will create a Grid .pdf
PDF
Go a crash course
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
PDF
Something about Golang
PDF
Go Containers
PDF
Go Containers
PDF
F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf
PDF
Burrowing through go! the book
PDF
Machine learning on Go Code
PDF
A Few of My Favorite (Python) Things
KEY
Programs as Strategies
going loopy - adventures in iteration with google go
going loopy - adventures in iteration with go - Eleanor McHugh - Codemotion M...
An introduction to functional programming with go
GoLightly: Building VM-Based Language Runtimes with Google Go
GoLightly - a customisable virtual machine written in Go
Go: It's Not Just For Google
Playing Go with Clojure
Implementing Software Machines in C and Go
C++ programming exercise
Description For this part of the assignment, you will create a Grid .pdf
Go a crash course
Let us C (by yashvant Kanetkar) chapter 3 Solution
Something about Golang
Go Containers
Go Containers
F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf
Burrowing through go! the book
Machine learning on Go Code
A Few of My Favorite (Python) Things
Programs as Strategies
Ad

More from Eleanor McHugh (20)

PDF
Go for the Paranoid Network Programmer, 2025 Edition
PDF
Y - Recursion The Hard Way GopherCon EU 2025
PDF
Never Say, Never Say Die! - the art of low-level Ruby and other Macabre Tales
PDF
[2024] An Introduction to Functional Programming with Go [Y Combinator Remix]...
PDF
[2023] Putting the R! in R&D.pdf
PDF
Generics, Reflection, and Efficient Collections
PDF
The Relevance of Liveness - Biometrics and Data Integrity
PDF
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
PDF
An introduction to functional programming with Go [redux]
PDF
Implementing virtual machines in go & c 2018 redux
PDF
Identity & trust in Monitored Spaces
PDF
Don't Ask, Don't Tell - The Virtues of Privacy By Design
PDF
Don't ask, don't tell the virtues of privacy by design
PDF
Anonymity, identity, trust
PDF
Distributed Ledgers: Anonymity & Immutability at Scale
PDF
Finding a useful outlet for my many Adventures in go
PDF
Anonymity, trust, accountability
PDF
Implementing Virtual Machines in Ruby & C
PDF
Implementing Software Machines in Go and C
PDF
Encrypt all transports
Go for the Paranoid Network Programmer, 2025 Edition
Y - Recursion The Hard Way GopherCon EU 2025
Never Say, Never Say Die! - the art of low-level Ruby and other Macabre Tales
[2024] An Introduction to Functional Programming with Go [Y Combinator Remix]...
[2023] Putting the R! in R&D.pdf
Generics, Reflection, and Efficient Collections
The Relevance of Liveness - Biometrics and Data Integrity
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
An introduction to functional programming with Go [redux]
Implementing virtual machines in go & c 2018 redux
Identity & trust in Monitored Spaces
Don't Ask, Don't Tell - The Virtues of Privacy By Design
Don't ask, don't tell the virtues of privacy by design
Anonymity, identity, trust
Distributed Ledgers: Anonymity & Immutability at Scale
Finding a useful outlet for my many Adventures in go
Anonymity, trust, accountability
Implementing Virtual Machines in Ruby & C
Implementing Software Machines in Go and C
Encrypt all transports

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
A Presentation on Artificial Intelligence
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Encapsulation theory and applications.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
Machine Learning_overview_presentation.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectral efficient network and resource selection model in 5G networks
A Presentation on Artificial Intelligence
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
SOPHOS-XG Firewall Administrator PPT.pptx
MYSQL Presentation for SQL database connectivity
A comparative analysis of optical character recognition models for extracting...
Assigned Numbers - 2025 - Bluetooth® Document
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MIND Revenue Release Quarter 2 2025 Press Release
Encapsulation theory and applications.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Machine Learning_overview_presentation.pptx

Going Loopy - Adventures in Iteration with Google Go

  • 1. going loopy adventures in iteration with google go @feyeleanor Going Loopy http://slideshare.net/feyeleanor/1
  • 2. the task Going Loopy http://slides.games-with-brains.net/2
  • 3. 0: 0 1: 2 2: 4 3: 6 4: 8 Going Loopy http://slides.games-with-brains.net/3
  • 4. the conditional loop Going Loopy http://slides.games-with-brains.net/4
  • 5. package main import "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { fmt.Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/5
  • 6. package main import "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { fmt.Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/6
  • 7. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } 7Going Loopy http://slides.games-with-brains.net/
  • 8. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } 8Going Loopy http://slides.games-with-brains.net/
  • 9. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } 9Going Loopy http://slides.games-with-brains.net/
  • 10. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/10
  • 11. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/11
  • 12. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/12
  • 13. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/13
  • 14. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/14
  • 15. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/15
  • 16. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/16
  • 17. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } 17Going Loopy http://slides.games-with-brains.net/
  • 18. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/18
  • 19. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/19
  • 20. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/20
  • 21. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i := 0; i < len(s); i++ { Printf("%v: %vn", i, s[i]) } } Going Loopy http://slides.games-with-brains.net/21
  • 22. the infinite loop Going Loopy http://slides.games-with-brains.net/22
  • 23. package main import . "fmt" func main() { defer func() { recover() }() s := []int{0, 2, 4, 6, 8} i := 0 for { Printf("%v: %vn", i, s[i]) i++ } } Going Loopy http://slides.games-with-brains.net/23
  • 24. package main import . "fmt" func main() { defer func() { recover() }() s := []int{0, 2, 4, 6, 8} i := 0 for { Printf("%v: %vn", i, s[i]) i++ } } Going Loopy http://slides.games-with-brains.net/24
  • 25. package main import . "fmt" func main() { defer func() { recover() }() s := []int{0, 2, 4, 6, 8} i := 0 for { Printf("%v: %vn", i, s[i]) i++ } } Going Loopy http://slides.games-with-brains.net/25
  • 26. package main import . "fmt" func main() { defer func() { recover() }() s := []int{0, 2, 4, 6, 8} i := 0 for { Printf("%v: %vn", i, s[i]) i++ } } Going Loopy http://slides.games-with-brains.net/26
  • 27. package main import . "fmt" func main() { defer func() { recover() }() s := []int{0, 2, 4, 6, 8} i := 0 for { Printf("%v: %vn", i, s[i]) i++ } } Going Loopy http://slides.games-with-brains.net/27
  • 28. package main import . "fmt" func main() { defer func() { recover() }() s := []int{0, 2, 4, 6, 8} i := 0 for i := 0; ; i++ { Printf("%v: %vn", i, s[i]) i++ } } Going Loopy http://slides.games-with-brains.net/28
  • 29. the range Going Loopy http://slides.games-with-brains.net/29
  • 30. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i, v := range s { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/30
  • 31. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i, v := range s { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/31
  • 32. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i, v := range s { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/32
  • 33. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i, v := range s { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/33
  • 34. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} for i, v := range s { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/34
  • 35. a functional interlude 35Going Loopy http://slides.games-with-brains.net/
  • 36. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s []int) { for i, v := range s { Printf("%v: %vn", i, v) } } 36Going Loopy http://slides.games-with-brains.net/
  • 37. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s []int) { for i, v := range s { Printf("%v: %vn", i, v) } } 37Going Loopy http://slides.games-with-brains.net/
  • 38. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s []int) { for i, v := range s { Printf("%v: %vn", i, v) } } 38Going Loopy http://slides.games-with-brains.net/
  • 39. package main import . "fmt" func main() { print_slice(0, 2, 4, 6, 8) } func print_slice(s ...int) { for i, v := range s { Printf("%v: %vn", i, v) } } 39Going Loopy http://slides.games-with-brains.net/
  • 40. asserting type Going Loopy http://slides.games-with-brains.net/40
  • 41. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { for i, v := range s.([]int) { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/41
  • 42. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { for i, v := range s.([]int) { Printf("%v: %vn", i, v) } } Going Loopy http://slides.games-with-brains.net/42
  • 43. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { if s, ok := s.([]int); ok { for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/43
  • 44. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { if s, ok := s.([]int); ok { for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/44
  • 45. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { if s, ok := s.([]int); ok { for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/45
  • 46. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { if s, ok := s.([]int); ok { for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/46
  • 47. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { switch s := s.(type) { case []int: for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/47
  • 48. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { switch s := s.(type) { case []int: for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/48
  • 49. package main import . "fmt" func main() { print_slice([]int{0, 2, 4, 6, 8}) } func print_slice(s interface{}) { switch s := s.(type) { case []int: for i, v := range s { Printf("%v: %vn", i, v) } } } Going Loopy http://slides.games-with-brains.net/49
  • 51. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 51Going Loopy http://slides.games-with-brains.net/
  • 52. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 52Going Loopy http://slides.games-with-brains.net/
  • 53. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 53Going Loopy http://slides.games-with-brains.net/
  • 54. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 54Going Loopy http://slides.games-with-brains.net/
  • 55. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 55Going Loopy http://slides.games-with-brains.net/
  • 56. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 56Going Loopy http://slides.games-with-brains.net/
  • 57. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_slice(func(i int) int { return s[i] }) } func print_slice(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } } } 57Going Loopy http://slides.games-with-brains.net/
  • 59. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 59Going Loopy http://slides.games-with-brains.net/
  • 60. package main import . "fmt" func main() { c := make(chan int, 16) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 60Going Loopy http://slides.games-with-brains.net/
  • 61. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 61Going Loopy http://slides.games-with-brains.net/
  • 62. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 62Going Loopy http://slides.games-with-brains.net/
  • 63. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 63Going Loopy http://slides.games-with-brains.net/
  • 64. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 64Going Loopy http://slides.games-with-brains.net/
  • 65. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 65Going Loopy http://slides.games-with-brains.net/
  • 66. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 66Going Loopy http://slides.games-with-brains.net/
  • 67. package main import . "fmt" func main() { c := make(chan int) go func() { for _, v := range []int{0, 2, 4, 6, 8} { c <- v } close(c) }() Printf("elements: %vn", print_channel(c)) } func print_channel(c chan int) (i int) { for v := range c { Printf("%v: %vn", i, v) i++ } return } 67Going Loopy http://slides.games-with-brains.net/
  • 68. upon reflection 68Going Loopy http://slides.games-with-brains.net/
  • 69. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } case []int: for i, v := range s { Printf("%v: %vn", i, v) } } } 69Going Loopy http://slides.games-with-brains.net/
  • 70. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } case []int: for i, v := range s { Printf("%v: %vn", i, v) } } } 70Going Loopy http://slides.games-with-brains.net/
  • 71. package main import . "fmt" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := s.(type) { case func(int) int: for i := 0; i < 5; i++ { Printf("%v: %vn", i, s(i)) } case []int: for i, v := range s { Printf("%v: %vn", i, v) } } } 71Going Loopy http://slides.games-with-brains.net/
  • 72. package main import . "fmt" import "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := reflect.ValueOf(s); s.Kind() { case reflect.Func: for i := 0; i < 5; i++ { p := []reflect.Value{ reflect.ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface{}) } case reflect.Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i)) } } } 72Going Loopy http://slides.games-with-brains.net/
  • 73. package main import . "fmt" import "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := reflect.ValueOf(s); s.Kind() { case reflect.Func: for i := 0; i < 5; i++ { p := []reflect.Value{ reflect.ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case reflect.Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 73Going Loopy http://slides.games-with-brains.net/
  • 74. package main import . "fmt" import "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := reflect.ValueOf(s); s.Kind() { case reflect.Func: for i := 0; i < 5; i++ { p := []reflect.Value{ reflect.ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case reflect.Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 74Going Loopy http://slides.games-with-brains.net/
  • 75. package main import . "fmt" import "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := reflect.ValueOf(s); s.Kind() { case reflect.Func: for i := 0; i < 5; i++ { p := []reflect.Value{ reflect.ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case reflect.Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 75Going Loopy http://slides.games-with-brains.net/
  • 76. package main import . "fmt" import "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := reflect.ValueOf(s); s.Kind() { case reflect.Func: for i := 0; i < 5; i++ { p := []reflect.Value{ reflect.ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case reflect.Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 76Going Loopy http://slides.games-with-brains.net/
  • 77. package main import . "fmt" import . "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := ValueOf(s); s.Kind() { case Func: for i := 0; i < 5; i++ { p := []Value{ ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 77Going Loopy http://slides.games-with-brains.net/
  • 78. package main import . "fmt" import . "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := ValueOf(s); s.Kind() { case Func: for i := 0; i < 5; i++ { p := []Value{ ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 78Going Loopy http://slides.games-with-brains.net/
  • 79. package main import . "fmt" import . "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := ValueOf(s); s.Kind() { case Func: for i := 0; i < 5; i++ { p := []Value{ ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 79Going Loopy http://slides.games-with-brains.net/
  • 80. package main import . "fmt" import . "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := ValueOf(s); s.Kind() { case Func: for i := 0; i < 5; i++ { p := []Value{ ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 80Going Loopy http://slides.games-with-brains.net/
  • 81. package main import . "fmt" import . "reflect" func main() { s := []int{0, 2, 4, 6, 8} print_values(s) print_values(func(i int) int { return s[i] }) } func print_values(s interface{}) { switch s := ValueOf(s); s.Kind() { case Func: for i := 0; i < 5; i++ { p := []Value{ ValueOf(i) } Printf("%v: %vn", i, s.Call(p)[0].Interface()) } case Slice: for i := 0; i < s.Len(); i++ { Printf("%v: %vn", i, s.Index(i).Interface()) } } } 81Going Loopy http://slides.games-with-brains.net/
  • 82. user-defined type 82Going Loopy http://slides.games-with-brains.net/
  • 83. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 83Going Loopy http://slides.games-with-brains.net/
  • 84. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 84Going Loopy http://slides.games-with-brains.net/
  • 85. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 85Going Loopy http://slides.games-with-brains.net/
  • 86. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 86Going Loopy http://slides.games-with-brains.net/
  • 87. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 87Going Loopy http://slides.games-with-brains.net/
  • 88. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 88Going Loopy http://slides.games-with-brains.net/
  • 89. package main import . "fmt" type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 89Going Loopy http://slides.games-with-brains.net/
  • 90. package main import . "fmt" type Iterable interface { Each(func(interface{})) } type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 90Going Loopy http://slides.games-with-brains.net/
  • 91. package main import . "fmt" type Iterable interface { Each(func(interface{})) } type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 91Going Loopy http://slides.games-with-brains.net/
  • 92. package main import . "fmt" type Iterable interface { Each(func(interface{})) } type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 92Going Loopy http://slides.games-with-brains.net/
  • 93. package main import . "fmt" type Iterable interface { Each(func(interface{})) } type IterableSlice []int func (i IterableSlice) Each(f func(interface{})) { for _, v := range i { f(v) } } func main() { var s Iterable = IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 93Going Loopy http://slides.games-with-brains.net/
  • 94. package main import . "fmt" type Iterable interface { Each(func(interface{})) } type IterableLimit int func (i IterableLimit) Each(f func(interface{})) { for v := 0; v < i; v++ { f(v) } } func main() { var s Iterable = IterableLimit(5) i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } 94Going Loopy http://slides.games-with-brains.net/
  • 95. AND YES, I DO HAVE A BOOK IN THE WORKS IF THAT'S YOUR THING leanpub://GoNotebook