August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, you are going to see a naive way to sort natural numbers using goroutines. The name of the program is sillySort.go and it will be presented in two parts. The first part of sillySort.go is the following:
package main
import (
"fmt"
"os"
"strconv"
"sync"
"time"
)
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println(os.Args[0], "n1, n2, [n]")
return
}
var wg sync.WaitGroup
for _, arg := range arguments[1:] {
n, err := strconv.Atoi(arg)
if err != nil || n < 0 {
fmt.Print(". ")
continue
}
The second part of sillySort.go contains the following Go code:
wg.Add(1) go func(n int) { defer wg.Done() time.Sleep(time.Duration(n) * time.Second) fmt.Print(n, " ") }(n) } wg.Wait() fmt.Println() ...Read now
Unlock full access