A silly way to sort natural numbers
This commit is contained in:
parent
ef65fe99ee
commit
622dbc2c3c
1 changed files with 37 additions and 0 deletions
37
ch10/sillySort.go
Normal file
37
ch10/sillySort.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue