Answer the question
In order to leave comments, you need to log in
How do goroutines work?
I read the doc, read the habr, then re-read it again and still did not understand:
package main
import (
"fmt"
"os"
)
func writeToFile() {
file, err := os.Create("result.txt")
if err != nil {
fmt.Println("Cannot create file")
}
defer file.Close()
fmt.Fprintf(file, "Hello Readers of golangcode.com")
}
func main() {
go writeToFile()
for {}
}
Answer the question
In order to leave comments, you need to log in
Go has such a system variable as $GOMAXPROCS, by default it is equal to the number of processor cores. Its value determines how many processes will be started by the Go scheduler. Goroutines are shared between these processes. If one goroutine has taken all the computational time of one process (as in your case, an infinite for loop, because it is not interrupted by I / O operations or other cases of waiting for resources), then if $GOMAXPROCS=1, other goroutines will wait for the release of a single process.
When you change the loop to a timer, the moment time.Wait is called, the process is released and can execute another goroutine.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question