-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
36 lines (33 loc) · 849 Bytes
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"fmt"
"sort"
"strconv"
)
func main() {
var userInputSlice = make([]int, 0, 3)
for {
fmt.Printf("Please, enter an integer number to store or 'X' to exit:\n")
// scan user input
var userInput string
fmt.Scan(&userInput)
// if user input equals X then exit
if userInput == "X" {
break
}
// convert user input into integer
userInputNumber, err := strconv.Atoi(userInput)
if err != nil {
fmt.Printf("Could not convert your \"input\" %s to integer. ", userInput)
continue
}
// store int(X) in slice
userInputSlice = append(userInputSlice, userInputNumber)
// sort the slice
sort.Slice(userInputSlice, func(i, j int) bool {
return userInputSlice[i] < userInputSlice[j]
})
// print the current state of the slice
fmt.Printf("Currently stored numbers: %v\n", userInputSlice)
}
}