Code snippet below,
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(1)
var s string
done := make(chan bool)
go func() {
fmt.Scanln(&s)
fmt.Println(s)
done <- true
}()
var i int
for i = 0; i < 1e10; i++ {
}
fmt.Println(i)
<-done
}
Run it, quickly type a few chars e.g. abcd
before the for
loop ends; finally hit Enter
. To my knowledge there is none of yield points available within the for
loop for Gosched
to switch the main goroutine to the subroutine, why had it promptly printed abcd
even before the for
loop was finished?
abcd10000000000
abcd