-
Notifications
You must be signed in to change notification settings - Fork 0
/
questionnaire.go
60 lines (48 loc) · 1.02 KB
/
questionnaire.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 (
"flag"
"fmt"
"math/rand"
"os"
"time"
"gopkg.in/yaml.v3"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type T struct {
Questions []struct {
Q string
A []string
}
}
func main() {
// Define CLI flags.
filePath := flag.String("file", "", "path of the yaml file with questions")
numQuestions := flag.Int("n", 10, "# of questions to ask")
flag.Parse()
// Print out the defaults.
if len(*filePath) == 0 {
fmt.Println("Usage: questionnaire.go -file")
flag.PrintDefaults()
os.Exit(1)
}
// Read the questions file.
data, err := os.ReadFile(*filePath)
check(err)
// The holder for the decoded yaml file.
t := T{}
// Decode the questions file.
check(yaml.Unmarshal([]byte(data), &t))
count := len(t.Questions)
fmt.Printf("\n%v questions found!\n\n", count)
// Generate new random sequence.
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
// Print out random questions.
for i := 0; i < *numQuestions; i++ {
fmt.Println(t.Questions[r1.Intn(count)].Q)
}
}