-
Notifications
You must be signed in to change notification settings - Fork 0
/
inpututils.go
52 lines (44 loc) · 909 Bytes
/
inpututils.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package go_utils
import (
"bufio"
"fmt"
"os"
"strings"
)
func GetInt() int {
var tmp int
fmt.Scan(&tmp)
return tmp
}
func GetFloat() float64 {
var tmp float64
fmt.Scan(&tmp)
return tmp
}
func GetString() string {
var tmp string
fmt.Scan(&tmp)
return tmp
}
func GetLine() string {
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
return line
}
func ReadChar(prompt string, interfaces ...interface{}) (string, error) {
fmt.Printf(prompt, interfaces...)
reader := bufio.NewReader(os.Stdin)
r, _, err := reader.ReadRune()
return string(r), err
}
// TODO: this might... just be wrong
func Readline(prompt string, interfaces ...interface{}) (string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(prompt, interfaces...)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
input = strings.Trim(input, "\n\r")
return input, nil
}