Sobes.tech
Junior — Senior

Estimating program runtime with 10 goroutines

livecode

Task condition

It is necessary to determine how long it will take for the program to finish if 10 goroutines are started, each calling the function printNumber, which pauses for one second before printing the number.

package main

import (
    "fmt"
    "time"
)

func main() {
    for idx := 0; idx < 10; idx++ {
        go func() {
            printNumber(idx)
        }()
    }
}

func printNumber(val int) {
    time.Sleep(time.Second)
    fmt.Println(val)
}