Created
April 7, 2020 16:20
-
-
Save doron2402/a99fe4f8055b6bc87a8a0867386bb8d1 to your computer and use it in GitHub Desktop.
Go routines example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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