forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ask.go
60 lines (53 loc) · 1.08 KB
/
ask.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
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"gopkg.in/AlecAivazis/survey.v1"
)
// the questions to ask
var simpleQs = []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
},
{
Name: "color",
Prompt: &survey.Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green", "yellow"},
Default: "yellow",
},
Validate: survey.Required,
},
}
func main() {
fmt.Println("Asking many.")
// a place to store the answers
ans := struct {
Name string
Color string
}{}
err := survey.Ask(simpleQs, &ans)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Asking one.")
answer := ""
err = survey.AskOne(simpleQs[0].Prompt, &answer, nil)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("Answered with %v.\n", answer)
fmt.Println("Asking one with validation.")
vAns := ""
err = survey.AskOne(&survey.Input{Message: "What is your name?"}, &vAns, survey.Required)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("Answered with %v.\n", vAns)
}