summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2017-04-02 22:50:34 +0200
committerPatrick Simianer <p@simianer.de>2017-04-02 22:50:34 +0200
commit833f7f1905bafb28574affc99056695c04390323 (patch)
treef3d41ef18db9c92cb6ccca6c9b9de27ec71ce64b
parentc2ade58bb868e72ba560553b3f72188453c55e7a (diff)
go
-rw-r--r--go/Makefile3
-rw-r--r--go/channels.go38
-rw-r--r--go/goroutines.go24
3 files changed, 65 insertions, 0 deletions
diff --git a/go/Makefile b/go/Makefile
new file mode 100644
index 0000000..80c6771
--- /dev/null
+++ b/go/Makefile
@@ -0,0 +1,3 @@
+%: %.go
+ go build $^
+
diff --git a/go/channels.go b/go/channels.go
new file mode 100644
index 0000000..03bcf68
--- /dev/null
+++ b/go/channels.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func pinger(c chan<- string) {
+ for i := 0; ; i++ {
+ c <- "ping"
+ }
+}
+
+func ponger(c chan string) {
+ for i := 0; ; i++ {
+ c <- "pong"
+ }
+}
+
+func printer(c <-chan string) {
+ for {
+ msg := <- c
+ fmt.Println(msg)
+ time.Sleep(time.Second * 1)
+ }
+}
+
+func main() {
+ var c chan string = make(chan string)
+
+ go pinger(c)
+ go ponger(c)
+ go printer(c)
+
+ var input string
+ fmt.Scanln(&input)
+}
+
diff --git a/go/goroutines.go b/go/goroutines.go
new file mode 100644
index 0000000..1eb1eb9
--- /dev/null
+++ b/go/goroutines.go
@@ -0,0 +1,24 @@
+package main
+
+import (
+ "fmt"
+ "time"
+ "math/rand"
+)
+
+func f(n int) {
+ for i := 0; i < 10; i++ {
+ fmt.Println(n, ":", i)
+ amt := time.Duration(rand.Intn(250))
+ time.Sleep(time.Millisecond * amt)
+ }
+}
+
+func main() {
+ for i := 0; i < 10; i++ {
+ go f(i)
+ }
+ var input string
+ fmt.Scanln(&input)
+}
+