going loopy - adventures in iteration with google go

1,246 views

Published on

A sideways introduction to programming in go via the medium of for loops

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,246
On SlideShare
0
From Embeds
0
Number of Embeds
94
Actions
Shares
0
Downloads
30
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

going loopy - adventures in iteration with google go

  1. 1. going loopy adventures in iteration with google go ! ! @feyeleanor Going Loopy !1 http://slideshare.net/feyeleanor/
  2. 2. the conditional loop Going Loopy !2 http://slides.games-with-brains.net/
  3. 3. 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 !3 http://slides.games-with-brains.net/
  4. 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]) } } ! ! 0: 1: 2: 3: 4: Going Loopy 0 2 4 6 8 !4 http://slides.games-with-brains.net/
  5. 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 !5 http://slides.games-with-brains.net/
  6. 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 !6 http://slides.games-with-brains.net/
  7. 7. 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 !7 http://slides.games-with-brains.net/
  8. 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]) } } Going Loopy !8 http://slides.games-with-brains.net/
  9. 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]) } } Going Loopy !9 http://slides.games-with-brains.net/
  10. 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 !10 http://slides.games-with-brains.net/
  11. 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 !11 http://slides.games-with-brains.net/
  12. 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 !12 http://slides.games-with-brains.net/
  13. 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 !13 http://slides.games-with-brains.net/
  14. 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 !14 http://slides.games-with-brains.net/
  15. 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 !15 http://slides.games-with-brains.net/
  16. 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 !16 http://slides.games-with-brains.net/
  17. 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]) } } Going Loopy !17 http://slides.games-with-brains.net/
  18. 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 !18 http://slides.games-with-brains.net/
  19. 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 !19 http://slides.games-with-brains.net/
  20. 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 !20 http://slides.games-with-brains.net/
  21. 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 !21 http://slides.games-with-brains.net/
  22. 22. 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 !22 http://slides.games-with-brains.net/
  23. 23. 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 !23 http://slides.games-with-brains.net/
  24. 24. the infinite loop Going Loopy !24 http://slides.games-with-brains.net/
  25. 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 !25 http://slides.games-with-brains.net/
  26. 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 !26 http://slides.games-with-brains.net/
  27. 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 !27 http://slides.games-with-brains.net/
  28. 28. 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 !28 http://slides.games-with-brains.net/
  29. 29. 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 !29 http://slides.games-with-brains.net/
  30. 30. 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 !30 http://slides.games-with-brains.net/
  31. 31. 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 !31 http://slides.games-with-brains.net/
  32. 32. the range Going Loopy !32 http://slides.games-with-brains.net/
  33. 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 !33 http://slides.games-with-brains.net/
  34. 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 !34 http://slides.games-with-brains.net/
  35. 35. 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 !35 http://slides.games-with-brains.net/
  36. 36. 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 !36 http://slides.games-with-brains.net/
  37. 37. 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 !37 http://slides.games-with-brains.net/
  38. 38. 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 !38 http://slides.games-with-brains.net/
  39. 39. a functional interlude Going Loopy !39 http://slides.games-with-brains.net/
  40. 40. 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) } } Going Loopy !40 http://slides.games-with-brains.net/
  41. 41. 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) } } Going Loopy !41 http://slides.games-with-brains.net/
  42. 42. 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) } } Going Loopy !42 http://slides.games-with-brains.net/
  43. 43. 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) } } Going Loopy !43 http://slides.games-with-brains.net/
  44. 44. 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) } } Going Loopy !44 http://slides.games-with-brains.net/
  45. 45. 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) } } Going Loopy !45 http://slides.games-with-brains.net/
  46. 46. 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) } } Going Loopy !46 http://slides.games-with-brains.net/
  47. 47. asserting type Going Loopy !47 http://slides.games-with-brains.net/
  48. 48. 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 !48 http://slides.games-with-brains.net/
  49. 49. 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 !49 http://slides.games-with-brains.net/
  50. 50. 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 !50 http://slides.games-with-brains.net/
  51. 51. 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 !51 http://slides.games-with-brains.net/
  52. 52. 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 !52 http://slides.games-with-brains.net/
  53. 53. 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 !53 http://slides.games-with-brains.net/
  54. 54. 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 !54 http://slides.games-with-brains.net/
  55. 55. 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 !55 http://slides.games-with-brains.net/
  56. 56. 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 !56 http://slides.games-with-brains.net/
  57. 57. 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 !57 http://slides.games-with-brains.net/
  58. 58. 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 !58 http://slides.games-with-brains.net/
  59. 59. 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 !59 http://slides.games-with-brains.net/
  60. 60. 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 !60 http://slides.games-with-brains.net/
  61. 61. 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 !61 http://slides.games-with-brains.net/
  62. 62. 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 !62 http://slides.games-with-brains.net/
  63. 63. closures Going Loopy !63 http://slides.games-with-brains.net/
  64. 64. 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)) } } } Going Loopy !64 http://slides.games-with-brains.net/
  65. 65. 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)) } } } Going Loopy !65 http://slides.games-with-brains.net/
  66. 66. 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)) } } } Going Loopy !66 http://slides.games-with-brains.net/
  67. 67. 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)) } } } Going Loopy !67 http://slides.games-with-brains.net/
  68. 68. 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)) } } } Going Loopy !68 http://slides.games-with-brains.net/
  69. 69. 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)) } } } Going Loopy !69 http://slides.games-with-brains.net/
  70. 70. 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)) } } } Going Loopy !70 http://slides.games-with-brains.net/
  71. 71. 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)) } } } Going Loopy !71 http://slides.games-with-brains.net/
  72. 72. 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)) } } } Going Loopy !72 http://slides.games-with-brains.net/
  73. 73. 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)) } } } Going Loopy !73 http://slides.games-with-brains.net/
  74. 74. channels Going Loopy !74 http://slides.games-with-brains.net/
  75. 75. 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 } Going Loopy !75 http://slides.games-with-brains.net/
  76. 76. 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 } Going Loopy !76 http://slides.games-with-brains.net/
  77. 77. 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 } Going Loopy !77 http://slides.games-with-brains.net/
  78. 78. 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 } Going Loopy !78 http://slides.games-with-brains.net/
  79. 79. 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 } Going Loopy !79 http://slides.games-with-brains.net/
  80. 80. 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 } Going Loopy !80 http://slides.games-with-brains.net/
  81. 81. 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 } Going Loopy !81 http://slides.games-with-brains.net/
  82. 82. 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 } Going Loopy !82 http://slides.games-with-brains.net/
  83. 83. 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 } Going Loopy !83 http://slides.games-with-brains.net/
  84. 84. 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 } Going Loopy !84 http://slides.games-with-brains.net/
  85. 85. upon reflection Going Loopy !85 http://slides.games-with-brains.net/
  86. 86. 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] * 2 }) } ! 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) } } } Going Loopy !86 http://slides.games-with-brains.net/
  87. 87. 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] * 2 }) } ! 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) } } } Going Loopy !87 http://slides.games-with-brains.net/
  88. 88. 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] * 2 }) } ! 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) } } } Going Loopy !88 http://slides.games-with-brains.net/
  89. 89. 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] * 2 }) } ! 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) } } } Going Loopy !89 http://slides.games-with-brains.net/
  90. 90. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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)) } } } Going Loopy !90 http://slides.games-with-brains.net/
  91. 91. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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)) } } } Going Loopy !91 http://slides.games-with-brains.net/
  92. 92. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !92 http://slides.games-with-brains.net/
  93. 93. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !93 http://slides.games-with-brains.net/
  94. 94. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !94 http://slides.games-with-brains.net/
  95. 95. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !95 http://slides.games-with-brains.net/
  96. 96. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !96 http://slides.games-with-brains.net/
  97. 97. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !97 http://slides.games-with-brains.net/
  98. 98. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !98 http://slides.games-with-brains.net/
  99. 99. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !99 http://slides.games-with-brains.net/
  100. 100. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !100 http://slides.games-with-brains.net/
  101. 101. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !101 http://slides.games-with-brains.net/
  102. 102. 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: p := []Value{ ValueOf(i) } for i := 0; i < 5; 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()) } } } Going Loopy !102 http://slides.games-with-brains.net/
  103. 103. user-defined type Going Loopy !103 http://slides.games-with-brains.net/
  104. 104. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !104 http://slides.games-with-brains.net/
  105. 105. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !105 http://slides.games-with-brains.net/
  106. 106. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !106 http://slides.games-with-brains.net/
  107. 107. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !107 http://slides.games-with-brains.net/
  108. 108. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !108 http://slides.games-with-brains.net/
  109. 109. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !109 http://slides.games-with-brains.net/
  110. 110. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !110 http://slides.games-with-brains.net/
  111. 111. 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() { s := IterableSlice{ 0, 2, 4, 6, 8 } i := 0 s.Each(func(v interface{}) { Printf("%v: %vn", i, v) i++ }) } Going Loopy !111 http://slides.games-with-brains.net/
  112. 112. you now know go Going Loopy 112 http://slides.games-with-brains.net/
  113. 113. #golang Going Loopy 113 http://slides.games-with-brains.net/
  114. 114. http://golang.org/ Going Loopy 114 http://slides.games-with-brains.net/

×