Middle+
package main import ( "fmt" "time" ) func main() { } func printNumber(n int) { time.Sleep(time.Second) fmt.Println(n) }
sobes.tech AI
Answer from AI
In this code, a function printNumber is defined, which outputs a number with a 1-second delay. However, it is not called in the main function.
If the goal is to output several numbers with a delay, you can launch multiple goroutines to run them in parallel, for example:
func main() {
for i := 1; i <= 5; i++ {
go printNumber(i)
}
time.Sleep(6 * time.Second) // Wait for goroutines to finish
}
This way, the numbers will be output almost simultaneously, rather than sequentially with a delay. If you call printNumber without goroutines, the output will be sequential with a 1-second delay between numbers.
This demonstrates basic working with goroutines and delays in Go.