Sobes.tech
Junior — Senior

Simultaneous output of numbers while preserving order

livecode

Task condition

It is necessary to modify the program so that the printNumber function runs in multiple goroutines, but the numbers from 1 to 10 are output strictly sequentially (1, 2, 3, ..., 10) with a one-second delay before each output.

package main

import (
        "fmt"
        "time"
        )

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

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