-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
41 lines (33 loc) · 865 Bytes
/
card.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
package main
import (
"log"
"os"
"strings"
)
type Card struct {
question string
answer string
}
func getCardsList(inputFilePath string) []Card {
cards := []Card{}
rawData, err := os.ReadFile(inputFilePath)
if err != nil {
log.Fatal(err.Error())
}
//split each card into a slice of strings
cardsStrings := strings.Split(string(rawData), "####")
for _, cardString := range cardsStrings {
var card Card
//split each card string into a slice containing question and answer strings
questionAnswerSlice := strings.Split(cardString, "##")
//indicates end of array, so break to avoid out of bounds index error
if len(questionAnswerSlice) == 1 {
break
}
//Add data to struct and append it to cards slice
card.question = questionAnswerSlice[0]
card.answer = questionAnswerSlice[1]
cards = append(cards, card)
}
return cards
}