Junior — Middle
What exactly initiates the launch of a new goroutine in a Go program?
sobes.tech AI
Answer from AI
In Go, a new goroutine is launched using the keyword go, followed by a call to a function or method. This initiates the asynchronous execution of the function in a separate lightweight thread.
Example:
func sayHello() {
fmt.Println("Hello from goroutine")
}
func main() {
go sayHello() // launch goroutine
// To see the result, you need to wait, for example, using time.Sleep
time.Sleep(time.Second)
}
Thus, the go operator specifically initiates the launch of a new goroutine.