Top Banner
going loopy adventures in iteration with go [email protected] Going Loopy slideshare.net/feyeleanor
107

Going Loopy: Adventures in Iteration with Go

Apr 16, 2017

Download

Software

Eleanor McHugh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Going Loopy: Adventures in Iteration with Go

going loopyadventures in iteration with go

[email protected]

Going Loopy slideshare.net/feyeleanor

Page 2: Going Loopy: Adventures in Iteration with Go

the conditional loop

Going Loopy slideshare.net/feyeleanor2

Page 3: Going Loopy: Adventures in Iteration with Go

package mainimport "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {fmt.Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor3

Page 4: Going Loopy: Adventures in Iteration with Go

package mainimport "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {fmt.Printf("%v: %v\n", i, s[i])

}}

0: 01: 22: 43: 64: 8

Going Loopy slideshare.net/feyeleanor4

Page 5: Going Loopy: Adventures in Iteration with Go

package mainimport "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {fmt.Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor5

Page 6: Going Loopy: Adventures in Iteration with Go

package mainimport "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {fmt.Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor6

Page 7: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

7Going Loopy slideshare.net/feyeleanor

Page 8: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

8Going Loopy slideshare.net/feyeleanor

Page 9: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

9Going Loopy slideshare.net/feyeleanor

Page 10: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor10

Page 11: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor11

Page 12: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor12

Page 13: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor13

Page 14: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor14

Page 15: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor15

Page 16: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

16Going Loopy slideshare.net/feyeleanor

Page 17: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor17

Page 18: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor18

Page 19: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor19

Page 20: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i := 0; i < len(s); i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor20

Page 21: Going Loopy: Adventures in Iteration with Go

the infinite loop

Going Loopy slideshare.net/feyeleanor21

Page 22: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}var i intfor {Printf("%v: %v\n", i, s[i])i++

}}

Going Loopy slideshare.net/feyeleanor22

Page 23: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}var i intfor {Printf("%v: %v\n", i, s[i])i++

}}

Going Loopy slideshare.net/feyeleanor23

Page 24: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}var i intfor {Printf("%v: %v\n", i, s[i])i++

}}

Going Loopy slideshare.net/feyeleanor24

Page 25: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}var i intfor {Printf("%v: %v\n", i, s[i])i++

}}

Going Loopy slideshare.net/feyeleanor25

Page 26: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}var i intfor {Printf("%v: %v\n", i, s[i])i++

}}

Going Loopy slideshare.net/feyeleanor26

Page 27: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}var i intfor {Printf("%v: %v\n", i, s[i])i++

}}

Going Loopy slideshare.net/feyeleanor27

Page 28: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {defer func() {recover()

}()s := []int{0, 2, 4, 6, 8}for i := 0; ; i++ {Printf("%v: %v\n", i, s[i])

}}

Going Loopy slideshare.net/feyeleanor28

Page 29: Going Loopy: Adventures in Iteration with Go

the range

Going Loopy slideshare.net/feyeleanor29

Page 30: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i, v := range s {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor30

Page 31: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i, v := range s {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor31

Page 32: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i, v := range s {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor32

Page 33: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i, v := range s {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor33

Page 34: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {s := []int{0, 2, 4, 6, 8}for i, v := range s {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor34

Page 35: Going Loopy: Adventures in Iteration with Go

a functional interlude

35Going Loopy slideshare.net/feyeleanor

Page 36: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s []int) {for i, v := range s {Printf("%v: %v\n", i, v)

}}

36Going Loopy slideshare.net/feyeleanor

Page 37: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s []int) {for i, v := range s {Printf("%v: %v\n", i, v)

}}

37Going Loopy slideshare.net/feyeleanor

Page 38: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s []int) {for i, v := range s {Printf("%v: %v\n", i, v)

}}

38Going Loopy slideshare.net/feyeleanor

Page 39: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s []int) {for i, v := range s {Printf("%v: %v\n", i, v)

}}

39Going Loopy slideshare.net/feyeleanor

Page 40: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice(0, 2, 4, 6, 8)

}

func print_slice(s ...int) {for i, v := range s {Printf("%v: %v\n", i, v)

}}

40Going Loopy slideshare.net/feyeleanor

Page 41: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice(0, 2, 4, 6, 8)

}

func print_slice(s ...int) {for i, v := range s {Printf("%v: %v\n", i, v)

}}

41Going Loopy slideshare.net/feyeleanor

Page 42: Going Loopy: Adventures in Iteration with Go

asserting type

Going Loopy slideshare.net/feyeleanor42

Page 43: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s interface{}) {for i, v := range s.([]int) {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor43

Page 44: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s interface{}) {for i, v := range s.([]int) {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor44

Page 45: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {print_slice([]int{0, 2, 4, 6, 8})

}

func print_slice(s interface{}) {for i, v := range s.([]int) {Printf("%v: %v\n", i, v)

}}

Going Loopy slideshare.net/feyeleanor45

Page 46: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor46

Page 47: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor47

Page 48: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor48

Page 49: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor49

Page 50: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor50

Page 51: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor51

Page 52: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor52

Page 53: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, v)

}}

}

Going Loopy slideshare.net/feyeleanor53

Page 54: Going Loopy: Adventures in Iteration with Go

closures

54Going Loopy slideshare.net/feyeleanor/

Page 55: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

55Going Loopy slideshare.net/feyeleanor

Page 56: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

56Going Loopy slideshare.net/feyeleanor

Page 57: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

57Going Loopy slideshare.net/feyeleanor

Page 58: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

58Going Loopy slideshare.net/feyeleanor

Page 59: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

59Going Loopy slideshare.net/feyeleanor

Page 60: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

60Going Loopy slideshare.net/feyeleanor

Page 61: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

61Going Loopy slideshare.net/feyeleanor

Page 62: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}}

}

62Going Loopy slideshare.net/feyeleanor

Page 63: Going Loopy: Adventures in Iteration with Go

channels

63Going Loopy slideshare.net/feyeleanor

Page 64: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

64Going Loopy slideshare.net/feyeleanor

Page 65: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

65Going Loopy slideshare.net/feyeleanor

Page 66: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

66Going Loopy slideshare.net/feyeleanor

Page 67: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

67Going Loopy slideshare.net/feyeleanor

Page 68: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

68Going Loopy slideshare.net/feyeleanor

Page 69: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

69Going Loopy slideshare.net/feyeleanor

Page 70: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

70Going Loopy slideshare.net/feyeleanor

Page 71: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

71Going Loopy slideshare.net/feyeleanor

Page 72: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

72Going Loopy slideshare.net/feyeleanor

Page 73: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

func main() {c := make(chan int)go func() {for _, v := range []int{0, 2, 4, 6, 8} {c <- v

}close(c)

}()Printf("elements: %v\n", print_channel(c))

}

func print_channel(c chan int) (i int) {for v := range c {Printf("%v: %v\n", i, v)i++

}return

}

73Going Loopy slideshare.net/feyeleanor

Page 74: Going Loopy: Adventures in Iteration with Go

user-defined type

74Going Loopy slideshare.net/feyeleanor

Page 75: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice[]intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

75Going Loopy slideshare.net/feyeleanor

Page 76: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice[]intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

76Going Loopy slideshare.net/feyeleanor

Page 77: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice[]intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

77Going Loopy slideshare.net/feyeleanor

Page 78: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice []intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

78Going Loopy slideshare.net/feyeleanor

Page 79: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice []intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

79Going Loopy slideshare.net/feyeleanor

Page 80: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice []intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

80Going Loopy slideshare.net/feyeleanor

Page 81: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(func(interface{}))

}

type IterableSlice []intfunc (i IterableSlice) Each(f func(interface{})) {

for _, v := range i {f(v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {i := 0iter.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})}

81Going Loopy slideshare.net/feyeleanor

Page 82: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(f func(int, interface{}))

}

type IterableSlice []intfunc (i IterableSlice) Each(f func(int, interface{})) {

for n, v := range i {f(n, v)

}}

func main() {print_values(IterableSlice{ 0, 2, 4, 6, 8 })

}

func print_values(iter Iterable) {iter.Each(func(i int, v interface{}) {Printf("%v: %v\n", i, v)

})}

82Going Loopy slideshare.net/feyeleanor

Page 83: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(interface{})

}

type IterableSlice []intfunc (i IterableSlice) Each(f interface{}) {

switch f := f.(type) {case func(interface{}):for _, v := range i { f(v) }

case func(int, interface{}):for n, v := range i { f(n, v) }

}}

func main() {s := IterableSlice{ 0, 2, 4, 6, 8 }i := 0s.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})s.Each(func(n int, v interface{}) {Printf("%v: %v\n", n, v)

})}

83Going Loopy slideshare.net/feyeleanor

Page 84: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(interface{})

}

type IterableSlice []intfunc (i IterableSlice) Each(f interface{}) {

switch f := f.(type) {case func(interface{}):for _, v := range i { f(v) }

case func(int, interface{}):for n, v := range i { f(n, v) }

}}

func main() {s := IterableSlice{ 0, 2, 4, 6, 8 }i := 0s.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})s.Each(func(n int, v interface{}) {Printf("%v: %v\n", n, v)

})}

84Going Loopy slideshare.net/feyeleanor

Page 85: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"

type Iterable interface {Each(interface{})

}

type IterableSlice []intfunc (i IterableSlice) Each(f interface{}) {

switch f := f.(type) {case func(interface{}):for _, v := range i { f(v) }

case func(int, interface{}):for n, v := range i { f(n, v) }

}}

func main() {s := IterableSlice{ 0, 2, 4, 6, 8 }i := 0s.Each(func(v interface{}) {Printf("%v: %v\n", i, v)i++

})s.Each(func(n int, v interface{}) {Printf("%v: %v\n", n, v)

})}

85Going Loopy slideshare.net/feyeleanor

Page 86: Going Loopy: Adventures in Iteration with Go

upon reflection

86Going Loopy slideshare.net/feyeleanor

Page 87: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}case []int:for i, v := range s {Printf("%v: %v\n", i, v)

}}

}

87Going Loopy slideshare.net/feyeleanor

Page 88: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}case []int:for i, v := range s {Printf("%v: %v\n", i, v)

}}

}

88Going Loopy slideshare.net/feyeleanor

Page 89: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}case []int:for i, v := range s {Printf("%v: %v\n", i, v)

}}

}

89Going Loopy slideshare.net/feyeleanor

Page 90: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s(i))

}case []int:for i, v := range s {Printf("%v: %v\n", i, v)

}}

}

90Going Loopy slideshare.net/feyeleanor

Page 91: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s.Call(p)[0].Interface())

}case Slice:for i := 0; i < s.Len(); i++ {Printf("%v: %v\n", i, s.Index(i).Interface())

}}

}

91Going Loopy slideshare.net/feyeleanor

Page 92: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s.Call(p)[0].Interface())

}case Slice:for i := 0; i < s.Len(); i++ {Printf("%v: %v\n", i, s.Index(i).Interface())

}}

}

92Going Loopy slideshare.net/feyeleanor

Page 93: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s.Call(p)[0].Interface())

}case Slice:for i := 0; i < s.Len(); i++ {Printf("%v: %v\n", i, s.Index(i).Interface())

}}

}

93Going Loopy slideshare.net/feyeleanor

Page 94: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s.Call(p)[0].Interface())

}case Slice:for i := 0; i < s.Len(); i++ {Printf("%v: %v\n", i, s.Index(i).Interface())

}}

}

94Going Loopy slideshare.net/feyeleanor

Page 95: Going Loopy: Adventures in Iteration with Go

package mainimport . "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: %v\n", i, s.Call(p)[0].Interface())

}case Slice:for i := 0; i < s.Len(); i++ {Printf("%v: %v\n", i, s.Index(i).Interface())

}}

}

95Going Loopy slideshare.net/feyeleanor

Page 96: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

96Going Loopy slideshare.net/feyeleanor

Page 97: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

97Going Loopy slideshare.net/feyeleanor

Page 98: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

98Going Loopy slideshare.net/feyeleanor

Page 99: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

99Going Loopy slideshare.net/feyeleanor

Page 100: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

100Going Loopy slideshare.net/feyeleanor

Page 101: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

101Going Loopy slideshare.net/feyeleanor

Page 102: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

102Going Loopy slideshare.net/feyeleanor

Page 103: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

103Going Loopy slideshare.net/feyeleanor

Page 104: Going Loopy: Adventures in Iteration with Go

package mainimport . "fmt"import . "reflect"

func main() {s := []int{0, 2, 4, 6, 8}print_values(s)print_values(func(i int) int {

return s[i]})print_values(func(i int) (v int, ok bool) {

defer func() {ok = recover() == nil

}()v = s[i]return

})}

func print_values(s interface{}) {switch s := ValueOf(s); s.Kind() {case Func:

print_func(s)case Slice:

for i := 0; i < s.Len(); i++ {print_value(i, s.Index(i))

}}

}

func print_func(s Value) {switch s.Type().NumOut() {case 1:

p := make([]Value, 1)for i := 0; i < 5; i++ {

p[0] = ValueOf(i)print_value(i, s.Call(p)[0])

}case 2:

each(s, func(i int, v Value) {print_value(i, v)

})default:

panic(s.Interface())}

}

func each(s Value, f func(int, Value)) {var i intp := []Value{ ValueOf(0) }r := s.Call(p)for r[1].Bool() {

f(i, r[0])i++p[0] = ValueOf(i)r = s.Call(p)

}}

func print_value(i int, v Value) {Printf("%v: %v\n", i, v.Interface())

}

104Going Loopy slideshare.net/feyeleanor

Page 105: Going Loopy: Adventures in Iteration with Go

you now know go

Going Loopy 112 slideshare.net/feyeleanor

Page 106: Going Loopy: Adventures in Iteration with Go

#golang

Going Loopy 113 slideshare.net/feyeleanor

Page 107: Going Loopy: Adventures in Iteration with Go

http://golang.org/

Going Loopy 114 slideshare.net/feyeleanor