Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of github.com:lagomeetup/talks

  • Loading branch information...
commit b575f5011a95006de3c22edd096d34a44b8ebca9 2 parents a77db9b + 76069b9
Miki Tebeka authored March 20, 2013

Showing 51 changed files with 1,359 additions and 3 deletions. Show diff stats Hide diff stats

  1. 3  2012/go-stdlib-part-1-io.slide
  2. BIN  2012/go-stdlib-part-1/bin/present
  3. 17  2013/go-stdlib-part-2/I_like_to_spawn.go
  4. 14  2013/go-stdlib-part-2/ch_of_ch.go
  5. 22  2013/go-stdlib-part-2/channel_copy.go
  6. 16  2013/go-stdlib-part-2/channel_one.go
  7. 21  2013/go-stdlib-part-2/channel_range.go
  8. 15  2013/go-stdlib-part-2/channel_read_on_close.go
  9. 18  2013/go-stdlib-part-2/channel_read_on_close_other.go
  10. 22  2013/go-stdlib-part-2/channel_reference.go
  11. 14  2013/go-stdlib-part-2/channel_simple.go
  12. 14  2013/go-stdlib-part-2/channel_simple_fix.go
  13. 22  2013/go-stdlib-part-2/function_literal_gotcha.go
  14. 29  2013/go-stdlib-part-2/function_literal_gotcha_fix.go
  15. 19  2013/go-stdlib-part-2/function_literals.go
  16. 15  2013/go-stdlib-part-2/go_1k_one_thread.go
  17. 11  2013/go-stdlib-part-2/go_1k_one_thread_2.go
  18. 22  2013/go-stdlib-part-2/go_1k_one_thread_runtime.go
  19. 24  2013/go-stdlib-part-2/go_1k_one_thread_yield.go
  20. 14  2013/go-stdlib-part-2/main_simple.go
  21. 15  2013/go-stdlib-part-2/main_simple_fix.go
  22. 22  2013/go-stdlib-part-2/max_procs.go
  23. 20  2013/go-stdlib-part-2/max_procs_2.go
  24. 14  2013/go-stdlib-part-2/python_1k_threads.py
  25. 25  2013/go-stdlib-part-2/select_timeout.go
  26. 12  2013/go-stdlib-part-2/ticker_example.go
  27. 12  2013/go-stdlib-part-2/time_format.go
  28. 10  2013/go-stdlib-part-2/time_sleep.go
  29. 19  2013/go-stdlib-part-2/wait_group_panic.go
  30. 19  2013/go-stdlib-part-2/wait_group_simple.go
  31. 19  2013/go-stdlib-part-2/wait_group_simple_fix.go
  32. 308  2013/go-stdlib-part2-io-concurrency.slide
  33. 147  2013/go-stdlib-part3-reflection.slide
  34. 30  2013/go-stdlib-part3/reflect_array.go
  35. 25  2013/go-stdlib-part3/reflect_call.go
  36. 36  2013/go-stdlib-part3/reflect_change_struct.go
  37. 24  2013/go-stdlib-part3/reflect_chann.go
  38. 42  2013/go-stdlib-part3/reflect_interface.go
  39. 17  2013/go-stdlib-part3/reflect_interface_wrong.go
  40. 31  2013/go-stdlib-part3/reflect_kind.go
  41. 24  2013/go-stdlib-part3/reflect_map.go
  42. 24  2013/go-stdlib-part3/reflect_new.go
  43. 36  2013/go-stdlib-part3/reflect_settable.go
  44. 15  2013/go-stdlib-part3/reflect_slice.go
  45. 32  2013/go-stdlib-part3/reflect_struct.go
  46. 11  2013/go-stdlib-part3/reflection1.go
  47. 30  2013/go-stdlib-part3/reflection2.go
  48. BIN  2013/nopanic.jpg
  49. BIN  2013/panic.jpg
  50. BIN  2013/wat_grande.jpg
  51. 11  README.md
3  2012/go-stdlib-part-1-io.slide
@@ -13,9 +13,10 @@ Part 1: I/O
13 13
 Fabrizio Milo
14 14
 Los Angeles Meetup Group -  November 20 2012 
15 15
 @fabmilo
16  
-mistobaan@gmail.com
  16
+lagomeetup@gmail.com
17 17
 http://www.meetup.com/Los-Angeles-Gophers/
18 18
 
  19
+
19 20
 * What will be covered:
20 21
 - io
21 22
 - os 
BIN  2012/go-stdlib-part-1/bin/present
Binary file not shown
17  2013/go-stdlib-part-2/I_like_to_spawn.go
... ...
@@ -0,0 +1,17 @@
  1
+package main
  2
+
  3
+import (
  4
+	"runtime"
  5
+)
  6
+
  7
+func main() {
  8
+	ch := make(chan int)
  9
+	ch2 := make(chan int)
  10
+	for {
  11
+		go func() {
  12
+			ch <- 0
  13
+		}()
  14
+		runtime.Gosched()
  15
+	}
  16
+	ch2 <- 0
  17
+}
14  2013/go-stdlib-part-2/ch_of_ch.go
... ...
@@ -0,0 +1,14 @@
  1
+package main
  2
+
  3
+
  4
+type ch2 chan chan interface{}
  5
+
  6
+
  7
+func main() {
  8
+	mych2 := make(ch2,1)
  9
+	ch := make(chan interface{},1)
  10
+	ch <- 1
  11
+	mych2 <- ch
  12
+	<- <- mych2
  13
+	close(mych2)
  14
+}
22  2013/go-stdlib-part-2/channel_copy.go
... ...
@@ -0,0 +1,22 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+type MyType struct {
  8
+	state int
  9
+}
  10
+
  11
+func main() {
  12
+     // START1 OMIT
  13
+	my_channel := make(chan MyType, 1)
  14
+	reference := MyType{}
  15
+	reference.state = 100 // HL
  16
+	my_channel <- reference
  17
+	reference.state = 200 // HL
  18
+	fmt.Printf("%v\n", reference)
  19
+	received := <-my_channel
  20
+	fmt.Printf("%v\n", received)
  21
+	// END1 OMIT
  22
+}
16  2013/go-stdlib-part-2/channel_one.go
... ...
@@ -0,0 +1,16 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func main() {
  8
+	// START1 OMIT
  9
+	blocking := make(chan int, 1)
  10
+	go func () { blocking <- 1 }()
  11
+	// go func () { blocking <- 1 }()
  12
+	<-blocking
  13
+	<-blocking
  14
+	fmt.Printf("Completed\n")
  15
+	// END1 OMIT
  16
+}
21  2013/go-stdlib-part-2/channel_range.go
... ...
@@ -0,0 +1,21 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func main() {
  8
+
  9
+	blocking := make(chan int, 5)
  10
+
  11
+	for i := 0; i < 10; i++ {
  12
+		blocking <- i
  13
+	}
  14
+
  15
+	//close(blocking)
  16
+
  17
+	for value := range blocking {
  18
+		fmt.Printf("Completed with %v\n", value)
  19
+	}
  20
+
  21
+}
15  2013/go-stdlib-part-2/channel_read_on_close.go
... ...
@@ -0,0 +1,15 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func main() {
  8
+	// START1 OMIT
  9
+	blocking := make(chan int)
  10
+	// blocking := make(chan string)
  11
+	close(blocking)
  12
+	value := <-blocking
  13
+	fmt.Printf("Completed with %v\n", value)
  14
+	// END1 OMIT
  15
+}
18  2013/go-stdlib-part-2/channel_read_on_close_other.go
... ...
@@ -0,0 +1,18 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func main() {
  8
+	// START1 OMIT
  9
+	blocking := make(chan int, 1)
  10
+	// blocking <-1
  11
+	close(blocking)
  12
+	if value, ok := <-blocking; ok {
  13
+	   fmt.Printf("Completed with %v\n", value)
  14
+	} else {
  15
+	   fmt.Printf("Channel is closed\n")
  16
+	}
  17
+	// END1 OMIT
  18
+}
22  2013/go-stdlib-part-2/channel_reference.go
... ...
@@ -0,0 +1,22 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+// START1 OMIT
  8
+type MyType struct {
  9
+	state int
  10
+}
  11
+
  12
+func main() {
  13
+	my_channel := make(chan *MyType, 1)
  14
+	reference := new(MyType)
  15
+	reference.state = 100 // HL
  16
+	my_channel <- reference
  17
+	reference.state = 200 // HL
  18
+	fmt.Printf("%v\n", reference)
  19
+	received := <-my_channel
  20
+	fmt.Printf("%v\n", received)
  21
+	// END1 OMIT
  22
+}
14  2013/go-stdlib-part-2/channel_simple.go
... ...
@@ -0,0 +1,14 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func main() {
  8
+	// START1 OMIT
  9
+	blocking := make(chan int)
  10
+	blocking <- 1
  11
+	<-blocking
  12
+	fmt.Printf("Completed\n")
  13
+	// END1 OMIT
  14
+}
14  2013/go-stdlib-part-2/channel_simple_fix.go
... ...
@@ -0,0 +1,14 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func main() {
  8
+	// START1 OMIT
  9
+	blocking := make(chan int)
  10
+	go func() { blocking <- 1 }()
  11
+	<-blocking
  12
+	fmt.Printf("Completed\n")
  13
+	// END1 OMIT
  14
+}
22  2013/go-stdlib-part-2/function_literal_gotcha.go
... ...
@@ -0,0 +1,22 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+type Callback func()
  8
+
  9
+func main() {
  10
+	// START1 OMIT
  11
+	funcArray := make([]Callback, 0)
  12
+
  13
+	for i := 0; i < 10; i++ {
  14
+		aNewFunc := func() { fmt.Printf("%d\n", i) } // HL
  15
+		funcArray = append(funcArray, aNewFunc)
  16
+	}
  17
+
  18
+	for i := 0; i < 10; i++ {
  19
+		funcArray[i]()
  20
+	}
  21
+	// END1 OMIT
  22
+}
29  2013/go-stdlib-part-2/function_literal_gotcha_fix.go
... ...
@@ -0,0 +1,29 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+type Callback func()
  8
+
  9
+// START2 OMIT
  10
+func mkCallback(i int) Callback {
  11
+	return func() { fmt.Printf("%d\n", i) } // HL
  12
+}
  13
+
  14
+// END2 OMIT
  15
+
  16
+func main() {
  17
+	// START1 OMIT
  18
+	funcArray := make([]Callback, 0)
  19
+
  20
+	for i := 0; i < 10; i++ {
  21
+		aNewFunc := mkCallback(i) // HL
  22
+		funcArray = append(funcArray, aNewFunc)
  23
+	}
  24
+
  25
+	for i := 0; i < 10; i++ {
  26
+		funcArray[i]()
  27
+	}
  28
+	// END1 OMIT
  29
+}
19  2013/go-stdlib-part-2/function_literals.go
... ...
@@ -0,0 +1,19 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+// START1 OMIT
  8
+type Callback func() // HL
  9
+// END1 OMIT
  10
+
  11
+func main() {
  12
+	//START2 OMIT
  13
+	var my_function Callback // HL
  14
+	my_function = func() {
  15
+		fmt.Printf("%d\n", 10)
  16
+	}
  17
+	my_function()
  18
+	//END2 OMIT
  19
+}
15  2013/go-stdlib-part-2/go_1k_one_thread.go
... ...
@@ -0,0 +1,15 @@
  1
+package main
  2
+
  3
+func main() {
  4
+	// START1 OMIT
  5
+	for i := 0; i < 1000; i++ {
  6
+		go func() {
  7
+			for {
  8
+			}
  9
+		}()
  10
+	}
  11
+
  12
+	for {
  13
+	}
  14
+	// END1 OMIT
  15
+}
11  2013/go-stdlib-part-2/go_1k_one_thread_2.go
... ...
@@ -0,0 +1,11 @@
  1
+package main
  2
+
  3
+func main() {
  4
+     // START1 OMIT
  5
+     for i:= 0; i < 1000; i++ {
  6
+       go func() {  for {}  } ()
  7
+     }
  8
+
  9
+     fmt.Scanf("Wait For It:%d", &i)
  10
+     // END1 OMIT
  11
+}
22  2013/go-stdlib-part-2/go_1k_one_thread_runtime.go
... ...
@@ -0,0 +1,22 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"runtime"
  6
+)
  7
+
  8
+func main() {
  9
+	// START1 OMIT
  10
+	for i := 0; i < 1000; i++ {
  11
+		go func() {
  12
+			for {
  13
+			}
  14
+		}()
  15
+		fmt.Printf("GoRoutines: %d \n", runtime.NumGoroutine())
  16
+	}
  17
+
  18
+	for {
  19
+		fmt.Printf("GoRoutines: %d \n", runtime.NumGoroutine())
  20
+	}
  21
+	// END1 OMIT
  22
+}
24  2013/go-stdlib-part-2/go_1k_one_thread_yield.go
... ...
@@ -0,0 +1,24 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"runtime"
  6
+)
  7
+
  8
+func main() {
  9
+	// START1 OMIT
  10
+	for i := 0; i < 1000; i++ {
  11
+		go func() {
  12
+			for {
  13
+             			runtime.Gosched() // HL
  14
+			}
  15
+		}()
  16
+   	fmt.Printf("GoRoutines: %d \n", runtime.NumGoroutine() )
  17
+	}
  18
+
  19
+	for {
  20
+		fmt.Printf("GoRoutines: %d \n", runtime.NumGoroutine() )
  21
+         			runtime.Gosched() // HL
  22
+	}
  23
+	// END1 OMIT
  24
+}
14  2013/go-stdlib-part-2/main_simple.go
... ...
@@ -0,0 +1,14 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func status() {
  8
+	fmt.Printf("It works !")
  9
+}
  10
+
  11
+
  12
+func main() {
  13
+	go status()
  14
+}
15  2013/go-stdlib-part-2/main_simple_fix.go
... ...
@@ -0,0 +1,15 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+func status() {
  8
+	fmt.Printf("It works !")
  9
+}
  10
+
  11
+
  12
+func main() {
  13
+	go status()
  14
+	for {}
  15
+}
22  2013/go-stdlib-part-2/max_procs.go
... ...
@@ -0,0 +1,22 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"runtime"
  6
+)
  7
+
  8
+// START1 OMIT
  9
+func MaxParallelism() int {
  10
+	maxProcs := runtime.GOMAXPROCS(0)
  11
+	numCPU := runtime.NumCPU()
  12
+	if maxProcs > numCPU {
  13
+		return maxProcs
  14
+	}
  15
+	return numCPU
  16
+}
  17
+// END1 OMIT
  18
+
  19
+func main() {
  20
+	runtime.GOMAXPROCS(MaxParallelism())
  21
+	fmt.Printf ("Parallel unit: %d\n",MaxParallelism())
  22
+}
20  2013/go-stdlib-part-2/max_procs_2.go
... ...
@@ -0,0 +1,20 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+)
  6
+
  7
+// START1 OMIT
  8
+func MaxParallelism() int {
  9
+	maxProcs := runtime.GOMAXPROCS(0)
  10
+	numCPU := runtime.NumCPU()
  11
+	if maxProcs < numCPU {
  12
+		return maxProcs
  13
+	}
  14
+	return numCPU
  15
+}
  16
+// END1 OMIT
  17
+
  18
+func main() {
  19
+	fmt.Printf ("Parallel unit: %d\n",MaxParallelism())
  20
+}
14  2013/go-stdlib-part-2/python_1k_threads.py
... ...
@@ -0,0 +1,14 @@
  1
+
  2
+import thread
  3
+
  4
+def loopme(*args):
  5
+    while True:
  6
+        pass
  7
+
  8
+def main():
  9
+    for i in xrange(1000):
  10
+        thread.start_new_thread(loopme, (None,))
  11
+    input()
  12
+    
  13
+main()
  14
+
25  2013/go-stdlib-part-2/select_timeout.go
... ...
@@ -0,0 +1,25 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"time"
  6
+)
  7
+
  8
+func main() {
  9
+	c := make(chan int)
  10
+
  11
+	handle := func() {
  12
+		time.Sleep(time.Second * 2)
  13
+		c <- 1
  14
+	}
  15
+	go handle()
  16
+
  17
+	select {
  18
+	case m := <-c:
  19
+		fmt.Printf("ok: %d\n", m)
  20
+	case <-time.After(500 * time.Second):
  21
+		fmt.Println("timed out")
  22
+	default:
  23
+		fmt.Println(".")
  24
+	}
  25
+}
12  2013/go-stdlib-part-2/ticker_example.go
... ...
@@ -0,0 +1,12 @@
  1
+package main
  2
+
  3
+import (
  4
+	"time"
  5
+	"fmt"
  6
+)
  7
+
  8
+func main() {
  9
+	for _ = range time.Tick( time.Millisecond * 500 ) {
  10
+		fmt.Printf("Tick ...")
  11
+	}
  12
+}
12  2013/go-stdlib-part-2/time_format.go
... ...
@@ -0,0 +1,12 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"time"
  6
+	"github.com/tebeka/strftime"
  7
+)
  8
+
  9
+func main() {
  10
+	fmt.Println(time.Now().Format("2006 Jan 01 02 15 04 05 MST -0700"))
  11
+	fmt.Println(strftime.Format("%Y %b %m %d %H %M %S %Z", time.Now()))
  12
+}
10  2013/go-stdlib-part-2/time_sleep.go
... ...
@@ -0,0 +1,10 @@
  1
+package main
  2
+
  3
+import (
  4
+	"time"
  5
+)
  6
+
  7
+
  8
+func main() {
  9
+	time.Sleep(10);
  10
+}
19  2013/go-stdlib-part-2/wait_group_panic.go
... ...
@@ -0,0 +1,19 @@
  1
+package main
  2
+
  3
+import "fmt"
  4
+import "sync"
  5
+
  6
+func main() {
  7
+	// START1  OMIT
  8
+	var V = []string{"a", "b", "c", "d"}
  9
+	var wg sync.WaitGroup
  10
+	for _, s := range V {
  11
+		wg.Add(1)
  12
+		go func(s string) { 
  13
+			defer wg.Done() // HL
  14
+			fmt.Printf(s)
  15
+		}(s) 
  16
+	}
  17
+	wg.Wait()
  18
+	// STOP1  OMIT
  19
+}
19  2013/go-stdlib-part-2/wait_group_simple.go
... ...
@@ -0,0 +1,19 @@
  1
+package main
  2
+
  3
+import "fmt"
  4
+import "sync"
  5
+
  6
+func main() {
  7
+	// START1 OMIT
  8
+	var V = []string{"a", "b", "c", "d"}
  9
+	var wg sync.WaitGroup // HL
  10
+	for _, s := range V {
  11
+		wg.Add(1) // HL
  12
+		go func() {
  13
+			fmt.Println(s)
  14
+			wg.Done()  // HL
  15
+		}()
  16
+	}
  17
+	wg.Wait()  // HL
  18
+	// END1 OMIT
  19
+}
19  2013/go-stdlib-part-2/wait_group_simple_fix.go
... ...
@@ -0,0 +1,19 @@
  1
+package main
  2
+
  3
+import "fmt"
  4
+import "sync"
  5
+
  6
+func main() {
  7
+	// START1  OMIT
  8
+	var V = []string{"a", "b", "c", "d"}
  9
+	var wg sync.WaitGroup
  10
+	for _, s := range V {
  11
+		wg.Add(1)
  12
+		go func(s string) { // HL
  13
+			fmt.Println(s)
  14
+			wg.Done()
  15
+		}(s) // HL
  16
+	}
  17
+	wg.Wait()
  18
+	// STOP1  OMIT
  19
+}
308  2013/go-stdlib-part2-io-concurrency.slide
... ...
@@ -0,0 +1,308 @@
  1
+The Go Standard Library 
  2
+Part 2: I/O & Concurrency
  3
+
  4
+# Go is a general-purpose language that bridges the gap between efficient
  5
+# statically typed languages and productive dynamic language. But it’s not just
  6
+# the language that makes Go special – Go has broad and consistent standard
  7
+# libraries and powerful but simple tools.
  8
+# 
  9
+# This talk gives an introduction to Go, followed by a tour of some real
  10
+# programs that demonstrate the power, scope, and simplicity of the Go
  11
+# programming environment.
  12
+
  13
+Fabrizio Milo
  14
+Los Angeles Meetup Group -  January 22 2013
  15
+@fabmilo
  16
+lagomeetup@gmail.com
  17
+http://www.meetup.com/Los-Angeles-Gophers/
  18
+
  19
+
  20
+* What we covered last time:
  21
+- io
  22
+- os 
  23
+- ioutils
  24
+- bufio
  25
+- bytes
  26
+- encoding/binary
  27
+- compress/gzip
  28
+
  29
+.link http://localhost:3999/2012/go-stdlib-part-1-io.slide#1
  30
+
  31
+* What will be covered in this session:
  32
+
  33
+- function literals
  34
+
  35
+- goroutines
  36
+
  37
+- channels
  38
+
  39
+- select
  40
+
  41
+- encoding/gob
  42
+
  43
+* Function Literals
  44
+
  45
+* Function Literals
  46
+
  47
+- Functions are first class citizen in go and they are type safe 
  48
+
  49
+.code go-stdlib-part-2/function_literals.go  /START1/,/END1/
  50
+
  51
+- and you can manipulate them as any other type
  52
+
  53
+.play go-stdlib-part-2/function_literals.go  /START2/,/END2/
  54
+
  55
+* Function Literals Gotchas
  56
+
  57
+What do you think the output will be ?
  58
+
  59
+.play go-stdlib-part-2/function_literal_gotcha.go  /START1/,/END1/
  60
+
  61
+How can we fix it ?
  62
+
  63
+* Function Literals Gotchas
  64
+
  65
+Function Factory
  66
+
  67
+.code go-stdlib-part-2/function_literal_gotcha_fix.go  /START2/,/END2/
  68
+
  69
+.play go-stdlib-part-2/function_literal_gotcha_fix.go  /START1/,/END1/
  70
+
  71
+
  72
+//* Concurrency: Do not communicate by sharing memory; instead, share memory by communicating.
  73
+
  74
+* Go Routines
  75
+* Go Routines
  76
+
  77
+- It is lightweight, costing little more than the allocation of stack space. 
  78
+
  79
+* Go Routines
  80
+
  81
+How to launch one
  82
+
  83
+    go <functionName>
  84
+
  85
+Easy right ?
  86
+
  87
+.play go-stdlib-part-2/main_simple.go
  88
+
  89
+* 
  90
+.image wat_grande.jpg 712 1000 
  91
+
  92
+* WAT
  93
+
  94
+Watch it is hilarious
  95
+
  96
+.link https://www.destroyallsoftware.com/talks/wat
  97
+
  98
+- ALL the gorutines are terminated when the main goroutine exits
  99
+
  100
+- The GO statement returns immediately
  101
+
  102
+* Goroutines
  103
+
  104
+.play go-stdlib-part-2/main_simple_fix.go
  105
+
  106
+THERE I FIX IT (for now)
  107
+
  108
+* Goroutines
  109
+
  110
+- It is lightweight, costing little more than the allocation of stack space. 
  111
+
  112
+- The stacks start small, so they are cheap, and grow by allocating (and freeing) heap storage as required
  113
+
  114
+- They are *NOT* threads
  115
+
  116
+* You can have 1000 goroutines running on one thread  
  117
+
  118
+* You don't believe it ?
  119
+
  120
+.image ./nopanic.jpg  712 1000
  121
+
  122
+* Go Routines
  123
+
  124
+.play go-stdlib-part-2/go_1k_one_thread.go  /START1/,/END1/
  125
+
  126
+* Go Routines
  127
+
  128
+- How do I know there are 1000 goroutines ?
  129
+
  130
+- How to use all the 64 processors of my machine ?
  131
+
  132
+* package runtime
  133
+
  134
+* runtime
  135
+
  136
+.link http://golang.org/pkg/runtime/
  137
+
  138
+.play go-stdlib-part-2/max_procs.go   /START1/,/END1/
  139
+
  140
+
  141
+* runtime.NumGoroutine
  142
+
  143
+- How do I know there are 1000 goroutines ?
  144
+
  145
+.link http://golang.org/pkg/runtime/#NumGoroutine
  146
+
  147
+.play go-stdlib-part-2/go_1k_one_thread_runtime.go   /START1/,/END1/
  148
+
  149
+* Go Routines
  150
+
  151
+As you saw the 1000 routines spinnig block quite heavely
  152
+
  153
+How can we yield ?
  154
+
  155
+* runtime.Gosched
  156
+
  157
+Explictly yield the routine 
  158
+
  159
+.link http://golang.org/pkg/runtime/#Gosched
  160
+
  161
+.play go-stdlib-part-2/go_1k_one_thread_yield.go   /START1/,/END1/
  162
+
  163
+* Go Routines
  164
+
  165
+How to wait for all the goroutines to finish ?
  166
+
  167
+Two main ways (other than an ininite loop):
  168
+
  169
+- sync.WaitGroups
  170
+
  171
+- channels
  172
+
  173
+* package sync
  174
+
  175
+* sync.WaitGroup
  176
+
  177
+
  178
+.link http://golang.org/pkg/sync/#WaitGroup
  179
+
  180
+.play go-stdlib-part-2/wait_group_simple.go  /START1/,/END1/
  181
+
  182
+What went wrong ?
  183
+
  184
+* sync.WaitGroup
  185
+
  186
+The variable is not closed
  187
+
  188
+.play go-stdlib-part-2/wait_group_simple_fix.go  /START1/,/STOP1/
  189
+
  190
+
  191
+* sync.WaitGroup
  192
+
  193
+What can we improve ?
  194
+
  195
+.play go-stdlib-part-2/wait_group_simple_fix.go  /START1/,/STOP1/
  196
+
  197
+* sync.WaitGroup
  198
+
  199
+Good Practice is to always defer to signaling the Done  
  200
+
  201
+.play go-stdlib-part-2/wait_group_panic.go  /START1/,/STOP1/
  202
+
  203
+* Channels
  204
+* Channels
  205
+
  206
+We already seen some channels example but never treated them 
  207
+
  208
+to create a channel:
  209
+
  210
+  mychan := make(chan <type>, <size>)
  211
+   
  212
+What does it mean <size> ?
  213
+
  214
+Let's see an example
  215
+
  216
+.play go-stdlib-part-2/channel_simple.go   /START1/,/END1/
  217
+
  218
+* 
  219
+
  220
+.image ./panic.jpg
  221
+
  222
+* 
  223
+No panic, and let's analyze the stack
  224
+
  225
+.image ./nopanic.jpg
  226
+
  227
+* Stack Trace:
  228
+
  229
+       throw: all goroutines are asleep - deadlock!
  230
+
  231
+       goroutine 1 [chan send]:
  232
+           main.main()
  233
+               [...] channel_simple.go:10+0x51
  234
+       goroutine 2 [syscall]:
  235
+           created by runtime.main
  236
+               /usr/local/Cellar/go/1.0.3/src/pkg/runtime/proc.c:221
  237
+
  238
+Just out of curiosity the second goroutine is the one that launches the main function
  239
+
  240
+.link http://golang.org/src/pkg/runtime/proc.c#221
  241
+
  242
+Of course the problem is that we are in a deadlock state
  243
+	       
  244
+How can we fix it ?
  245
+
  246
+* Channels Fix
  247
+We need to run it in another goroutine
  248
+
  249
+.play go-stdlib-part-2/channel_simple_fix.go   /START1/,/END1/
  250
+
  251
+* Channels  with 1 element 
  252
+
  253
+We consume the first but not the second
  254
+
  255
+.play go-stdlib-part-2/channel_one.go   /START1/,/END1/
  256
+
  257
+* Channels 
  258
+
  259
+What happens if we close a channel before consuming it ?
  260
+
  261
+.play go-stdlib-part-2/channel_read_on_close.go   /START1/,/END1/
  262
+
  263
+It returns the default value 
  264
+
  265
+Testing it with string ...
  266
+
  267
+* Channels
  268
+
  269
+How to differentiate the two cases ?
  270
+
  271
+.play go-stdlib-part-2/channel_read_on_close_other.go   /START1/,/END1/
  272
+
  273
+Channel has an alternative sintax
  274
+
  275
+* Channels gotchas
  276
+
  277
+.play go-stdlib-part-2/channel_reference.go   /START1/,/END1/
  278
+
  279
+* Channels gotchas
  280
+
  281
+Pay attention if the channel's element is not a primitive or an immutable object (i.e. string) 
  282
+
  283
+This Channel copies the *entire* struct 
  284
+
  285
+.play go-stdlib-part-2/channel_copy.go   /START1/,/END1/
  286
+
  287
+* Channels of Channels ?
  288
+
  289
+Because channel is a primitive you can pass channels around
  290
+
  291
+
  292
+* Let' introduce another stdlib package !
  293
+
  294
+* time
  295
+.link http://golang.org/pkg/time
  296
+
  297
+First method is of course:
  298
+
  299
+   time.Sleep(d Duration)
  300
+
  301
+.link http://golang.org/pkg/time#Sleep
  302
+
  303
+* Select
  304
+
  305
+
  306
+* Exercise
  307
+
  308
+* Thanks !
147  2013/go-stdlib-part3-reflection.slide
... ...
@@ -0,0 +1,147 @@
  1
+The Go Standard Library 
  2
+Part 3: Reflection
  3
+
  4
+Fabrizio Milo
  5
+Los Angeles Meetup Group -  February 19 2013
  6
+@fabmilo
  7
+lagomeetup@gmail.com
  8
+http://www.meetup.com/Los-Angeles-Gophers/
  9
+
  10
+
  11
+* What we covered last time:
  12
+
  13
+- function literals
  14
+
  15
+- goroutines
  16
+
  17
+- channels
  18
+
  19
+- select
  20
+
  21
+* What will be covered in this session:
  22
+
  23
+- reflection
  24
+
  25
+* Reflection in GO
  26
+
  27
+* Reflection in GO
  28
+
  29
+.link http://golang.org/doc/articles/laws_of_reflection.html
  30
+
  31
+Reflection in computing is the ability of a program to examine its own structure, particularly through types
  32
+
  33
+* Reflection in GO
  34
+
  35
+What you can do:
  36
+
  37
+- iterate ove array, slices, maps and structs
  38
+- calling methods and functions
  39
+- sending and receiving on channels
  40
+- allocating memory
  41
+
  42
+* The Laws of  Reflection
  43
+
  44
+- Reflection goes from interface value to reflection object
  45
+- Reflection goes from reflection object to interface value
  46
+- To modify a reflection object, the value must be settable
  47
+
  48
+* Interface
  49
+
  50
+An Interface has two components:
  51
+
  52
+- Type 
  53
+.link http://golang.org/pkg/reflect/#Type
  54
+
  55
+- Value 
  56
+.link http://golang.org/pkg/reflect/#Value
  57
+
  58
+
  59
+* Interface
  60
+
  61
+from a variable we can extract the Type and the Value with
  62
+
  63
+  v := 10
  64
+  T := reflect.TypeOf(v)
  65
+  V := reflect.ValueOf(v)
  66
+
  67
+.link http://golang.org/pkg/reflect/#TypeOf
  68
+.link http://golang.org/pkg/reflect/#ValueOf
  69
+
  70
+* Interface
  71
+
  72
+.play go-stdlib-part3/reflection1.go
  73
+
  74
+* 
  75
+.image wat_grande.jpg 712 1000 
  76
+
  77
+* Interface
  78
+
  79
+.play go-stdlib-part3/reflect_kind.go /START1/,/END1/
  80
+
  81
+* What is Settability
  82
+
  83
+- Settability is a property of a reflection Value, and not all reflection Values have it.
  84
+
  85
+.play go-stdlib-part3/reflect_settable.go /START1/,/END1/
  86
+.play go-stdlib-part3/reflect_settable.go /START2/,/END2/
  87
+.play go-stdlib-part3/reflect_settable.go /START3/,/END3/
  88
+
  89
+
  90
+* Iterate over Array
  91
+
  92
+.play go-stdlib-part3/reflect_array.go /START1/,/END1/
  93
+
  94
+* Iterate over Maps
  95
+
  96
+.play go-stdlib-part3/reflect_map.go /START1/,/END1/
  97
+
  98
+* Using Slices 
  99
+
  100
+.play go-stdlib-part3/reflect_slice.go /START1/,/END1/
  101
+
  102
+* Iterate over Struct
  103
+
  104
+Only Exported Fields of a Field are Settable
  105
+
  106
+.link http://golang.org/pkg/reflect/#StructField
  107
+.link http://golang.org/pkg/reflect/#StructTag
  108
+
  109
+* Iterate over Struct
  110
+
  111
+.play go-stdlib-part3/reflect_struct.go /START1/,/END1/
  112
+
  113
+* Change Struct Fields
  114
+
  115
+.play go-stdlib-part3/reflect_change_struct.go /START1/,/END1/
  116
+
  117
+* Iterate over interfaces
  118
+
  119
+How do you get the TypeOf(interface) ?
  120
+
  121
+.code go-stdlib-part3/reflect_interface_wrong.go /START1/,/END1/
  122
+.play go-stdlib-part3/reflect_interface_wrong.go /START2/,/END2/
  123
+
  124
+* Iterate over interfaces
  125
+
  126
+.link http://golang.org/pkg/reflect/#Method
  127
+
  128
+.code go-stdlib-part3/reflect_interface.go /START3/,/END3/
  129
+.play go-stdlib-part3/reflect_interface.go /START2/,/END2/
  130
+
  131
+
  132
+* Iterate over interfaces
  133
+
  134
+.play go-stdlib-part3/reflect_interface.go /START1/,/END1/
  135
+
  136
+* Calling Methods
  137
+
  138
+.play go-stdlib-part3/reflect_call.go /START1/,/END1/
  139
+
  140
+* Creating Channels
  141
+
  142
+.play go-stdlib-part3/reflect_chann.go /START1/,/END1/
  143
+
  144
+* Creating Structs
  145
+
  146
+.play go-stdlib-part3/reflect_new.go /START1/,/END1/
  147
+
30  2013/go-stdlib-part3/reflect_array.go
... ...
@@ -0,0 +1,30 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"reflect"
  6
+)
  7
+
  8
+// START1 OMIT
  9
+func meta(a interface{}) {
  10
+	pt := reflect.TypeOf(a)
  11
+	pv := reflect.ValueOf(a)
  12
+
  13
+	fmt.Printf("\n")
  14
+	for i := 0; i < pt.Len(); i++ {
  15
+		elementValue := pv.Index(i)
  16
+		fmt.Printf("%v:%v:%v:%v\n", i, elementValue.Interface(), elementValue.Type(), elementValue.Kind())
  17
+    }
  18
+    fmt.Printf("\n")
  19
+}
  20
+
  21
+func main() {
  22
+	a := [10]int{1,2,3,4,5,6,7,8,9,10}
  23
+
  24
+	meta(a)
  25
+   
  26
+    b := [3]interface{}{1,"IamString", struct { int }{0} }
  27
+
  28
+    meta(b)
  29
+// END1 OMIT
  30
+}
25  2013/go-stdlib-part3/reflect_call.go
... ...
@@ -0,0 +1,25 @@
  1
+package main
  2
+
  3
+import (
  4
+	"fmt"
  5
+	"reflect"
  6
+)
  7
+
  8
+// START1 OMIT
  9
+type Person struct{}
  10
+
  11
+func (p *Person) Run() {
  12
+	fmt.Println("person running")
  13
+}
  14
+
  15
+func doit(object interface{}, method interface{}) {
  16
+	v := reflect.ValueOf(object)
  17
+	f := reflect.ValueOf(method)
  18
+	f.Call([]reflect.Value{v})
  19
+}
  20
+
  21
+func main() {
  22
+	p := new(Person)
  23
+	doit(p, (*Person).Run)
  24
+	// END1 OMIT
  25
+}
36  2013/go-stdlib-part3/reflect_change_struct.go
... ...
@@ -0,0 +1,36 @@
  1
+package main