Skip to content

Instantly share code, notes, and snippets.

@index0h
Created December 30, 2016 16:51
Show Gist options
  • Save index0h/0d896c8494162258658d586a0ebe8ba4 to your computer and use it in GitHub Desktop.
Save index0h/0d896c8494162258658d586a0ebe8ba4 to your computer and use it in GitHub Desktop.
example goroutine
index0h@home-pc:/tmp$ cat test.go
package main
import (
"fmt"
"time"
)
func pinger(c chan string) {
for i := 0; ; i++ {
c <- "ping"
}
}
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 printer(c)
var input string
fmt.Scanln(&input)
}
index0h@home-pc:/tmp$ go run ./test.go
ping
ping
ping
ping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment