Skip to content

Instantly share code, notes, and snippets.

@santosh
Last active June 17, 2021 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santosh/a730493fcbc896b61df1af5c76bec3d7 to your computer and use it in GitHub Desktop.
Save santosh/a730493fcbc896b61df1af5c76bec3d7 to your computer and use it in GitHub Desktop.
progressively learning goroutines
package main
import (
"fmt"
"math/rand"
"time"
)
func boring(msg string, c chan string) {
for i := 0; ; i++ {
c <- fmt.Sprintf("%s %d", msg, i) // Expression to be sent can be any suitable value.
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
}
func main() {
c := make(chan string)
go boring("boring1", c)
go boring("boring2", c)
go boring("boring3", c)
for i := 0; i < 20; i++ {
fmt.Printf("You say: %q\n", <-c) // Receive expression is just a value.
}
fmt.Println("You're boring; I'm leaving.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment