Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

WIP

  • Loading branch information...
commit 9221e7f029db24bf787cebb181b538e3c1b24c24 1 parent a35eaca
@Mistobaan Mistobaan authored
Showing with 455 additions and 69 deletions.
  1. +2 −1  2012/go-stdlib-part-1-io.slide
  2. +22 −0 2013/go-stdlib-part-2/channel_copy.go
  3. +16 −0 2013/go-stdlib-part-2/channel_one.go
  4. +15 −0 2013/go-stdlib-part-2/channel_read_on_close.go
  5. +18 −0 2013/go-stdlib-part-2/channel_read_on_close_other.go
  6. +22 −0 2013/go-stdlib-part-2/channel_reference.go
  7. +14 −0 2013/go-stdlib-part-2/channel_simple.go
  8. +14 −0 2013/go-stdlib-part-2/channel_simple_fix.go
  9. +11 −11 2013/go-stdlib-part-2/function_literal_gotcha.go
  10. +14 −13 2013/go-stdlib-part-2/function_literal_gotcha_fix.go
  11. +2 −2 2013/go-stdlib-part-2/function_literals.go
  12. +18 −0 2013/go-stdlib-part-2/go_1k_one_thread.go
  13. +11 −0 2013/go-stdlib-part-2/go_1k_one_thread_2.go
  14. +14 −0 2013/go-stdlib-part-2/main_simple.go
  15. +21 −0 2013/go-stdlib-part-2/max_procs.go
  16. +14 −0 2013/go-stdlib-part-2/python_1k_threads.py
  17. +10 −0 2013/go-stdlib-part-2/time_sleep.go
  18. +19 −0 2013/go-stdlib-part-2/wait_group_panic.go
  19. +7 −6 2013/go-stdlib-part-2/wait_group_simple.go
  20. +6 −5 2013/go-stdlib-part-2/wait_group_simple_fix.go
  21. +185 −31 2013/go-stdlib-part2-io-concurrency.slide
  22. BIN  2013/nopanic.jpg
  23. BIN  2013/panic.jpg
View
3  2012/go-stdlib-part-1-io.slide
@@ -13,9 +13,10 @@ Part 1: I/O
Fabrizio Milo
Los Angeles Meetup Group - November 20 2012
@fabmilo
-mistobaan@gmail.com
+lagomeetup@gmail.com
http://www.meetup.com/Los-Angeles-Gophers/
+
* What will be covered:
- io
- os
View
22 2013/go-stdlib-part-2/channel_copy.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "fmt"
+)
+
+type MyType struct {
+ state int
+}
+
+func main() {
+ // START1 OMIT
+ my_channel := make(chan MyType, 1)
+ reference := MyType{}
+ reference.state = 100 // HL
+ my_channel <- reference
+ reference.state = 200 // HL
+ fmt.Printf("%v\n", reference)
+ received := <-my_channel
+ fmt.Printf("%v\n", received)
+ // END1 OMIT
+}
View
16 2013/go-stdlib-part-2/channel_one.go
@@ -0,0 +1,16 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START1 OMIT
+ blocking := make(chan int, 1)
+ go func () { blocking <- 1 }()
+ // go func () { blocking <- 1 }()
+ <-blocking
+ <-blocking
+ fmt.Printf("Completed\n")
+ // END1 OMIT
+}
View
15 2013/go-stdlib-part-2/channel_read_on_close.go
@@ -0,0 +1,15 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START1 OMIT
+ blocking := make(chan int)
+ // blocking := make(chan string)
+ close(blocking)
+ value := <-blocking
+ fmt.Printf("Completed with %v\n", value)
+ // END1 OMIT
+}
View
18 2013/go-stdlib-part-2/channel_read_on_close_other.go
@@ -0,0 +1,18 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START1 OMIT
+ blocking := make(chan int, 1)
+ // blocking <-1
+ close(blocking)
+ if value, ok := <-blocking; ok {
+ fmt.Printf("Completed with %v\n", value)
+ } else {
+ fmt.Printf("Channel is closed\n")
+ }
+ // END1 OMIT
+}
View
22 2013/go-stdlib-part-2/channel_reference.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "fmt"
+)
+
+// START1 OMIT
+type MyType struct {
+ state int
+}
+
+func main() {
+ my_channel := make(chan *MyType, 1)
+ reference := new(MyType)
+ reference.state = 100 // HL
+ my_channel <- reference
+ reference.state = 200 // HL
+ fmt.Printf("%v\n", reference)
+ received := <-my_channel
+ fmt.Printf("%v\n", received)
+ // END1 OMIT
+}
View
14 2013/go-stdlib-part-2/channel_simple.go
@@ -0,0 +1,14 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START1 OMIT
+ blocking := make(chan int)
+ blocking <- 1
+ <-blocking
+ fmt.Printf("Completed\n")
+ // END1 OMIT
+}
View
14 2013/go-stdlib-part-2/channel_simple_fix.go
@@ -0,0 +1,14 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START1 OMIT
+ blocking := make(chan int)
+ go func() { blocking <- 1 }()
+ <-blocking
+ fmt.Printf("Completed\n")
+ // END1 OMIT
+}
View
22 2013/go-stdlib-part-2/function_literal_gotcha.go
@@ -4,19 +4,19 @@ import (
"fmt"
)
-type Callback func()
+type Callback func()
func main() {
// START1 OMIT
- funcArray := make([]Callback,0)
+ funcArray := make([]Callback, 0)
- for i :=0 ; i < 10; i++ {
- aNewFunc := func() { fmt.Printf("%d\n",i) } // HL
- funcArray = append(funcArray, aNewFunc )
- }
-
- for i :=0 ; i < 10; i++ {
- funcArray[i]()
- }
- // END1 OMIT
+ for i := 0; i < 10; i++ {
+ aNewFunc := func() { fmt.Printf("%d\n", i) } // HL
+ funcArray = append(funcArray, aNewFunc)
+ }
+
+ for i := 0; i < 10; i++ {
+ funcArray[i]()
+ }
+ // END1 OMIT
}
View
27 2013/go-stdlib-part-2/function_literal_gotcha_fix.go
@@ -4,25 +4,26 @@ import (
"fmt"
)
-type Callback func()
+type Callback func()
// START2 OMIT
-func mkCallback( i int ) Callback {
- return func() { fmt.Printf("%d\n",i) } // HL
+func mkCallback(i int) Callback {
+ return func() { fmt.Printf("%d\n", i) } // HL
}
+
// END2 OMIT
func main() {
// START1 OMIT
- funcArray := make([]Callback,0)
+ funcArray := make([]Callback, 0)
+
+ for i := 0; i < 10; i++ {
+ aNewFunc := mkCallback(i) // HL
+ funcArray = append(funcArray, aNewFunc)
+ }
- for i :=0 ; i < 10; i++ {
- aNewFunc := mkCallback(i) // HL
- funcArray = append(funcArray, aNewFunc )
- }
-
- for i :=0 ; i < 10; i++ {
- funcArray[i]()
- }
- // END1 OMIT
+ for i := 0; i < 10; i++ {
+ funcArray[i]()
+ }
+ // END1 OMIT
}
View
4 2013/go-stdlib-part-2/function_literals.go
@@ -5,12 +5,12 @@ import (
)
// START1 OMIT
-type Callback func() // HL
+type Callback func() // HL
// END1 OMIT
func main() {
//START2 OMIT
- var my_function Callback // HL
+ var my_function Callback // HL
my_function = func() {
fmt.Printf("%d\n", 10)
}
View
18 2013/go-stdlib-part-2/go_1k_one_thread.go
@@ -0,0 +1,18 @@
+package main
+
+import (
+ "fmt"
+)
+
+func main() {
+ // START1 OMIT
+ for i := 0; i < 100; i++ {
+ go func() {
+ for {
+ }
+ }()
+ }
+ var end int
+ fmt.Scanf("Wait For It:%d", &end)
+ // END1 OMIT
+}
View
11 2013/go-stdlib-part-2/go_1k_one_thread_2.go
@@ -0,0 +1,11 @@
+package main
+
+func main() {
+ // START1 OMIT
+ for i:= 0; i < 1000; i++ {
+ go func() { for {} } ()
+ }
+
+ fmt.Scanf("Wait For It:%d", &i)
+ // END1 OMIT
+}
View
14 2013/go-stdlib-part-2/main_simple.go
@@ -0,0 +1,14 @@
+package main
+
+import (
+ "fmt"
+)
+
+func status() {
+ fmt.Printf("It works !")
+}
+
+
+func main() {
+ go status()
+}
View
21 2013/go-stdlib-part-2/max_procs.go
@@ -0,0 +1,21 @@
+package main
+
+import (
+ "fmt"
+ "runtime"
+)
+
+// START1 OMIT
+func MaxParallelism() int {
+ maxProcs := runtime.GOMAXPROCS(0)
+ numCPU := runtime.NumCPU()
+ if maxProcs < numCPU {
+ return maxProcs
+ }
+ return numCPU
+}
+// END1 OMIT
+
+func main() {
+ fmt.Printf ("Parallel unit: %d\n",MaxParallelism())
+}
View
14 2013/go-stdlib-part-2/python_1k_threads.py
@@ -0,0 +1,14 @@
+
+import thread
+
+def loopme(*args):
+ while True:
+ pass
+
+def main():
+ for i in xrange(1000):
+ thread.start_new_thread(loopme, (None,))
+ input()
+
+main()
+
View
10 2013/go-stdlib-part-2/time_sleep.go
@@ -0,0 +1,10 @@
+package main
+
+import (
+ "time"
+)
+
+
+func main() {
+ time.Sleep(10);
+}
View
19 2013/go-stdlib-part-2/wait_group_panic.go
@@ -0,0 +1,19 @@
+package main
+
+import "fmt"
+import "sync"
+
+func main() {
+ // START1 OMIT
+ var V = []string{"a", "b", "c", "d"}
+ var wg sync.WaitGroup
+ for _, s := range V {
+ wg.Add(1)
+ go func(s string) {
+ defer wg.Done() // HL
+ fmt.Printf(s)
+ }(s)
+ }
+ wg.Wait()
+ // STOP1 OMIT
+}
View
13 2013/go-stdlib-part-2/wait_group_simple.go
@@ -3,16 +3,17 @@ package main
import "fmt"
import "sync"
-var V = []string { "a", "b", "c", "d" }
-
func main() {
- var wg sync.WaitGroup
+ // START1 OMIT
+ var V = []string{"a", "b", "c", "d"}
+ var wg sync.WaitGroup // HL
for _, s := range V {
- wg.Add(1)
+ wg.Add(1) // HL
go func() {
fmt.Println(s)
- wg.Done()
+ wg.Done() // HL
}()
}
- done.Wait()
+ wg.Wait() // HL
+ // END1 OMIT
}
View
11 2013/go-stdlib-part-2/wait_group_simple_fix.go
@@ -3,16 +3,17 @@ package main
import "fmt"
import "sync"
-var V = []string { "a", "b", "c", "d" }
-
func main() {
+ // START1 OMIT
+ var V = []string{"a", "b", "c", "d"}
var wg sync.WaitGroup
for _, s := range V {
wg.Add(1)
- go func(s string) {
+ go func(s string) { // HL
fmt.Println(s)
wg.Done()
- }(s)
+ }(s) // HL
}
- done.Wait()
+ wg.Wait()
+ // STOP1 OMIT
}
View
216 2013/go-stdlib-part2-io-concurrency.slide
@@ -1,5 +1,5 @@
The Go Standard Library
-Part 1: I/O
+Part 2: I/O & Concurrency
# Go is a general-purpose language that bridges the gap between efficient
# statically typed languages and productive dynamic language. But it’s not just
@@ -13,9 +13,10 @@ Part 1: I/O
Fabrizio Milo
Los Angeles Meetup Group - January 22 2013
@fabmilo
-mistobaan@gmail.com
+lagomeetup@gmail.com
http://www.meetup.com/Los-Angeles-Gophers/
+
* What we covered last time:
- io
- os
@@ -31,10 +32,10 @@ http://www.meetup.com/Los-Angeles-Gophers/
- goroutines
-- select
-
- channels
+- select
+
- encoding/gob
* Function Literals
@@ -59,62 +60,215 @@ How can we fix it ?
Function Factory
-.play go-stdlib-part-2/function_literal_gotcha_fix.go /START2/,/END2/
+.code go-stdlib-part-2/function_literal_gotcha_fix.go /START2/,/END2/
.play go-stdlib-part-2/function_literal_gotcha_fix.go /START1/,/END1/
-* Concurrency
+* Concurrency: Do not communicate by sharing memory; instead, share memory by communicating.
+
+* Go Routines
-Do not communicate by sharing memory;
-instead, share memory by communicating.
+- It is lightweight, costing little more than the allocation of stack space.
+
+- The stacks start small, so they are cheap, and grow by allocating (and freeing) heap storage as required
+
+- They are *NOT* threads
* Go Routines
-It is lightweight, costing little more than the allocation of stack space.
+How to launch one
-And the stacks start small, so they are cheap, and grow by allocating (and freeing) heap storage as required
+ go <functionName>
-runtime.GOMAXPROCS(runtime.NumCPU())
+Easy right ?
-* Select
+.play go-stdlib-part-2/main_simple.go
-* Channels
-* io.MultiWrite
+* You can have 1000 goroutines running on one thread
-* encoding/gob
+* You don't believe it ?
-.link http://golang.org/pkg/encoding/gob/
-.link http://golang.org/doc/articles/gobs_of_data.html
+.image ./nopanic.jpg
+
+* Go Routines
+.play go-stdlib-part-2/go_1k_one_thread.go /START1/,/END1/
+
+- How do I know there are 1000 goroutines ?
-* https://github.com/eknkc/dateformat/
+
+How to wait for all the goroutines to finish ?
+
+- sync.WaitGroups
+
+- channels
* sync WaitGroup
.link http://golang.org/pkg/sync/#WaitGroup
- How to wait for all the goroutines to finish ?
+* sync.WaitGroup
+
+.play go-stdlib-part-2/wait_group_simple.go /START1/,/END1/
+
+What went wrong ?
- sync.WaitGroup
+* sync.WaitGroup
-.code go-stdlib-part-1/wait_group_simple.go /START2/,/STOP2/
+The variable is not closed
-- What went wrong ?
+.play go-stdlib-part-2/wait_group_simple_fix.go /START1/,/STOP1/
-.code go-stdlib-part-1/wait_group_simple_fix.go /START2/,/STOP2/
+
+* sync.WaitGroup
+
+What can we improve ?
+
+.play go-stdlib-part-2/wait_group_simple_fix.go /START1/,/STOP1/
+
+* sync.WaitGroup
+
+Good Practice is to always defer to signaling the Done
+
+.play go-stdlib-part-2/wait_group_panic.go /START1/,/STOP1/
* runtime
- func MaxParallelism() int {
- maxProcs := runtime.GOMAXPROCS(0)
- numCPU := runtime.NumCPU()
- if maxProcs < numCPU {
- return maxProcs
- }
- return numCPU
- }
+.link http://golang.org/pkg/runtime/#GOMAXPROCS
+
+.play go-stdlib-part-2/max_procs.go /START1/,/END1/
+
+explictly yield
+.link http://golang.org/pkg/runtime/#GoSched
+.link http://golang.org/pkg/runtime/#NumGoroutine
+
+* Channels
+
+We already seen some channels example but never treated them
+
+to create a channel:
+
+ mychan := make(chan <type>, <size>)
+
+What does it mean <size> ?
+
+Let's see an example
+
+.play go-stdlib-part-2/channel_simple.go /START1/,/END1/
+
+*
+
+.image ./panic.jpg
+
+*
+No panic, and let's analyze the stack
+
+.image ./nopanic.jpg
+
+* Stack Trace:
+
+ throw: all goroutines are asleep - deadlock!
+
+ goroutine 1 [chan send]:
+ main.main()
+ [...] channel_simple.go:10+0x51
+ goroutine 2 [syscall]:
+ created by runtime.main
+ /usr/local/Cellar/go/1.0.3/src/pkg/runtime/proc.c:221
+
+Just out of curiosity the second gorutine is the one that launches the
+
+.link http://golang.org/src/pkg/runtime/proc.c#221
+
+- The problem is that we are in a deadlock state
+
+* Channels Fix
+We need to run it in another goroutine
+
+.play go-stdlib-part-2/channel_simple_fix.go /START1/,/END1/
+
+* Channels with 1 element
+
+We consume the first but not the second
+
+.play go-stdlib-part-2/channel_one.go /START1/,/END1/
+
+* Channels
+
+What happens if we close a channel before consuming it ?
+
+.play go-stdlib-part-2/channel_read_on_close.go /START1/,/END1/
+
+It returns the default value
+
+Testing it with string ...
+
+* Channels
+
+How to differentiate the two cases ?
+
+.play go-stdlib-part-2/channel_read_on_close_other.go /START1/,/END1/
+
+Channel has an alternative sintax
+
+* Channels gotchas
+
+.play go-stdlib-part-2/channel_reference.go /START1/,/END1/
+
+* Channels gotchas
+
+Pay attention if the channel's element is not a primitive or an immutable object (i.e. string)
+
+This Channel copies the *entire* struct
+
+.play go-stdlib-part-2/channel_copy.go /START1/,/END1/
+
+* Channels of Channels ?
+
+Because channel is a primitive you can pass channels around
+
+
+* Let' introduce another stdlib package !
+
+* time
+.link http://golang.org/pkg/time
+
+First method is of course:
+
+ time.Sleep(d Duration)
+
+.link http://golang.org/pkg/time#Sleep
+
+What? Duration ?
+
+* Time Duration
+
+.link http://golang.org/pkg/time#Duration
+
+ type Duration int64
+
+Time duration is an int64 but we can't do:
+
+.play go-stdlib-part-2/time_sleep.go
+
+
+* Select
+
+* io.MultiWrite
+
+* encoding/gob
+
+.link http://golang.org/pkg/encoding/gob/
+.link http://golang.org/doc/articles/gobs_of_data.html
+
+
+* https://github.com/eknkc/dateformat/
+
+* Debugging Tricks
+- SIGQUIT
+- gdb
* Thanks !
View
BIN  2013/nopanic.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  2013/panic.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.