Skip to content

Instantly share code, notes, and snippets.

@doron2402
Created April 7, 2020 16:20
Show Gist options
  • Save doron2402/a99fe4f8055b6bc87a8a0867386bb8d1 to your computer and use it in GitHub Desktop.
Save doron2402/a99fe4f8055b6bc87a8a0867386bb8d1 to your computer and use it in GitHub Desktop.
Go routines example
package main
import (
"fmt"
"math/rand"
"time"
)
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
func main() {
// Goroutines
for i := 0; i < 10; i++ {
id := rnd.Intn(10) + 1
go func(id int) {
// print A
printA(id)
}(id)
go func(id int) {
// print B
printB(id)
}(id)
}
// Wait 100ms before shutting down
time.Sleep(100 * time.Microsecond)
}
func printA(num int) bool {
fmt.Printf("%d) A\n", num)
return num%2 == 0
}
func printB(num int) bool {
fmt.Printf("%d) B\n", num)
return num%2 == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment