-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
40 lines (35 loc) · 806 Bytes
/
input.go
File metadata and controls
40 lines (35 loc) · 806 Bytes
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
37
38
39
40
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func ReadInputYesNo() (bool, error) {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return false, err
}
fields := strings.Fields(input) // hacky way to remove whitespace characters
if len(fields) == 0 {
return false, fmt.Errorf("invalid input")
}
return strings.ToLower(fields[0]) == "y", nil
}
func ReadInputNumbers() ([]int, error) {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
check(err)
inputFields := strings.Fields(input)
var subsToRemove []int
for _, n := range inputFields {
i, err := strconv.ParseInt(n, 10, 64)
if err != nil {
return nil, err
}
subsToRemove = append(subsToRemove, int(i))
}
return subsToRemove, nil
}