summaryrefslogtreecommitdiff
path: root/go/channels.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/channels.go')
-rw-r--r--go/channels.go38
1 files changed, 38 insertions, 0 deletions
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)
+}
+