-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs.go
50 lines (45 loc) · 896 Bytes
/
inputs.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func Ask(question string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/n] ", question)
response, err := reader.ReadString('\n')
if err != nil {
return false
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
}
if response == "n" || response == "no" {
return false
}
fmt.Println("Please answer yes or no.")
}
}
func RequiredAsk(question, errorMessage string) bool {
for {
if Ask(question) {
return true
}
fmt.Println(errorMessage)
}
}
func GetInput(prompt string) string {
fmt.Print("\n", prompt, " ")
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading input: %v\n", err)
return ""
}
return ""
}
return scanner.Text()
}