Skip to content

Instantly share code, notes, and snippets.

@laidback
Created February 17, 2018 17:28
Show Gist options
  • Select an option

  • Save laidback/2b29ef137624a5d8cd7abd64650bce05 to your computer and use it in GitHub Desktop.

Select an option

Save laidback/2b29ef137624a5d8cd7abd64650bce05 to your computer and use it in GitHub Desktop.
go-routines created by laidback - https://repl.it/@laidback/go-routines
package main
import "os"
import "fmt"
import "time"
import "runtime/debug"
import "runtime/pprof"
var _ = os.Stdout
var _ = debug.PrintStack
var _ = pprof.Lookup
func ping(pings chan<- string, pongs <-chan string) {
dbg := 0
for played := 0; played < 5; played++ {
switch <- pongs {
case "start":
fmt.Println("start ping ...")
pings <- "ping"
default:
if dbg == 0 { debug.PrintStack(); dbg++ }
fmt.Println("ponged")
pings <- "ping"
}
}
fmt.Println("exiting ping")
}
func pong(pongs chan<- string, pings <-chan string) {
dbg := 0
for played := 0; played < 5; played++ {
switch <- pings {
case "start":
fmt.Println("start pong ...")
pongs <- "pong"
default:
if dbg == 0 { debug.PrintStack(); dbg++ }
fmt.Println("pinged")
pongs <- "pong"
}
}
fmt.Println("exiting pong")
}
func main() {
pings := make(chan string)
pongs := make(chan string)
go ping(pings, pongs)
go pong(pongs, pings)
pongs <- "start"
//debug.PrintStack()
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
seconds := 3
time.Sleep(time.Duration(seconds) * time.Second)
fmt.Println("Hello World")
// wait for user
// var input string
// fmt.Scanln(&input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment