Permalink
Browse files

WIP

  • Loading branch information...
Mistobaan committed Jan 23, 2013
1 parent a35eaca commit 9221e7f029db24bf787cebb181b538e3c1b24c24
@@ -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
@@ -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
+}
@@ -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
+}
@@ -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
+}
@@ -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
+}
@@ -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
+}
@@ -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
+}
@@ -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
+}
@@ -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
}
@@ -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
}
@@ -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)
}
@@ -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
+}
@@ -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
+}
@@ -0,0 +1,14 @@
+package main
+
+import (
+ "fmt"
+)
+
+func status() {
+ fmt.Printf("It works !")
+}
+
+
+func main() {
+ go status()
+}
@@ -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())
+}
@@ -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()
+
@@ -0,0 +1,10 @@
+package main
+
+import (
+ "time"
+)
+
+
+func main() {
+ time.Sleep(10);
+}
@@ -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
+}
@@ -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
}
@@ -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
}
Oops, something went wrong.

0 comments on commit 9221e7f

Please sign in to comment.