package main
import (
"fmt"
//"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
//runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world") // create a new goroutine
say("hello") // current goroutine
}
Why result is:
hello
hello
hello
hello
hello
Why is there no world
?
Answer: (edited:) If I do this, it is good now:
package main
import (
"fmt"
"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
//runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world") // create a new goroutine
runtime.Gosched()
say("hello") // current goroutine
}