Run the code snippet in for loop as a goroutine to finish in time.
Goroutines
Anonymous goroutines
Report a typo
Write a program in Go
package main
import (
"fmt"
"time"
)
func main() {
// do not modify this block of code
start := time.Now()
defer func() {
if time.Since(start) > time.Second*2 {
fmt.Println(10000)
}
}()
for i := 0; i < 5; i++ {
// run the code in the loop concurrently
? ?(i int) {
// this simulates long calculations
time.Sleep(time.Millisecond * 500)
fmt.Printf("%d ", i*i)
}(i)
}
time.Sleep(time.Second)
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.