go-learning/5concurrency/goroutines.go
2023-08-26 10:51:33 +03:00

39 lines
623 B
Go

package main
import (
"fmt"
"time"
)
func printX() {
for i := 0; i < 100; i++ {
fmt.Print("X")
}
}
func printY() {
for i := 0; i < 100; i++ {
fmt.Print("Y")
}
}
func main() {
fmt.Println("a goroutine is a **lightweight** thread managed by the go runtime")
printX()
fmt.Println()
printY()
fmt.Println()
fmt.Println("you should see first X's then Y's")
time.Sleep(100 * time.Millisecond)
fmt.Println()
fmt.Println()
go printX()
go printY()
fmt.Println("you should see random X's and Y's, this because concurrency")
time.Sleep(1 * time.Second)
// wait for prevent main goroutine to terminate
}