Flashcard CLI app in Golang
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
This is a simple flashcard app. With flags to control the sides of the flashcards and the number of flashcards in the session.
All the flashcards without any input at the prompt, are deleted from the map. And ones with an input, remain. The idea is that, the user marks the flashcard that they want to revise in case they had forgotten it, later in the session.
The outer loop, loops until the length of the map is >= 1
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"runtime"
"time"
"github.com/fatih/color"
)
var (
flagNoColor = flag.Bool("no-color", false, "disable color output")
csvPath = flag.String("src", "src.csv", "source")
side = flag.Int("s", -1, "side")
num = flag.Int("n", -1, "number")
)
var clear map[string]func()
func init()
clear = make(map[string]func())
clear["linux"] = func()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
clear["windows"] = func()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
func CallClear()
value, ok := clear[runtime.GOOS]
if ok
value()
else
panic("Panic!")
func random(min int, max int) int
return rand.Intn((max-min) + 1) + min
func main()
flag.Parse()
file, err := os.Open(*csvPath)
if err != nil
log.Println(err)
return
defer file.Close()
csvReader := csv.NewReader(file)
csvData, err := csvReader.ReadAll()
if err != nil
log.Println(err)
return
qaRaw := make(map[string]string, len(csvData))
for _, data := range csvData
qaRaw[data[0]] = data[1]
qaPair := make(map[string]string)
if *num != -1
i := 1
for one, two := range qaRaw
if i <= *num
qaPair[one] = two
i++
else
for one, two := range qaRaw
qaPair[one] = two
done := make(chan bool)
go func()
if *flagNoColor
color.NoColor = true
white := color.New(color.FgWhite)
boldWhite := white.Add(color.Bold)
qNum := 0
for len(qaPair) >= 1
for one, two := range qaPair
rand.Seed(time.Now().UnixNano())
randomNum := random(1, 2)
if *side != -1
randomNum = *side
var userInput string
CallClear()
boldWhite.Printf("n #t%d / %dn", qNum+1, qNum+len(qaPair))
qNum++
if randomNum == 1
boldWhite.Printf("n Qt%s", one)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", two)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
else if randomNum == 2
boldWhite.Printf("n Qt%s", two)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", one)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
CallClear()
done <- true
()
select
case <-done:
fmt.Println("blah blah blah")
Flashcards example data (.csv file)
"hola","hi"
"sÃÂ","yes"
"ÿqué pasa?","what's up?"
"ávamos!","let's go!"
"ásalud!","cheers!; bless you!"
"no","no"
"por favor","please"
"lo siento","I'm sorry"
"buenos dÃÂas","good morning"
"buenas noches","good night"
"gracias","thank you"
"vale","okay"
"hasta luego","see you later"
"adiós","goodbye"
"cómo","how (questions)"
go
add a comment |Â
up vote
5
down vote
favorite
This is a simple flashcard app. With flags to control the sides of the flashcards and the number of flashcards in the session.
All the flashcards without any input at the prompt, are deleted from the map. And ones with an input, remain. The idea is that, the user marks the flashcard that they want to revise in case they had forgotten it, later in the session.
The outer loop, loops until the length of the map is >= 1
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"runtime"
"time"
"github.com/fatih/color"
)
var (
flagNoColor = flag.Bool("no-color", false, "disable color output")
csvPath = flag.String("src", "src.csv", "source")
side = flag.Int("s", -1, "side")
num = flag.Int("n", -1, "number")
)
var clear map[string]func()
func init()
clear = make(map[string]func())
clear["linux"] = func()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
clear["windows"] = func()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
func CallClear()
value, ok := clear[runtime.GOOS]
if ok
value()
else
panic("Panic!")
func random(min int, max int) int
return rand.Intn((max-min) + 1) + min
func main()
flag.Parse()
file, err := os.Open(*csvPath)
if err != nil
log.Println(err)
return
defer file.Close()
csvReader := csv.NewReader(file)
csvData, err := csvReader.ReadAll()
if err != nil
log.Println(err)
return
qaRaw := make(map[string]string, len(csvData))
for _, data := range csvData
qaRaw[data[0]] = data[1]
qaPair := make(map[string]string)
if *num != -1
i := 1
for one, two := range qaRaw
if i <= *num
qaPair[one] = two
i++
else
for one, two := range qaRaw
qaPair[one] = two
done := make(chan bool)
go func()
if *flagNoColor
color.NoColor = true
white := color.New(color.FgWhite)
boldWhite := white.Add(color.Bold)
qNum := 0
for len(qaPair) >= 1
for one, two := range qaPair
rand.Seed(time.Now().UnixNano())
randomNum := random(1, 2)
if *side != -1
randomNum = *side
var userInput string
CallClear()
boldWhite.Printf("n #t%d / %dn", qNum+1, qNum+len(qaPair))
qNum++
if randomNum == 1
boldWhite.Printf("n Qt%s", one)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", two)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
else if randomNum == 2
boldWhite.Printf("n Qt%s", two)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", one)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
CallClear()
done <- true
()
select
case <-done:
fmt.Println("blah blah blah")
Flashcards example data (.csv file)
"hola","hi"
"sÃÂ","yes"
"ÿqué pasa?","what's up?"
"ávamos!","let's go!"
"ásalud!","cheers!; bless you!"
"no","no"
"por favor","please"
"lo siento","I'm sorry"
"buenos dÃÂas","good morning"
"buenas noches","good night"
"gracias","thank you"
"vale","okay"
"hasta luego","see you later"
"adiós","goodbye"
"cómo","how (questions)"
go
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
This is a simple flashcard app. With flags to control the sides of the flashcards and the number of flashcards in the session.
All the flashcards without any input at the prompt, are deleted from the map. And ones with an input, remain. The idea is that, the user marks the flashcard that they want to revise in case they had forgotten it, later in the session.
The outer loop, loops until the length of the map is >= 1
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"runtime"
"time"
"github.com/fatih/color"
)
var (
flagNoColor = flag.Bool("no-color", false, "disable color output")
csvPath = flag.String("src", "src.csv", "source")
side = flag.Int("s", -1, "side")
num = flag.Int("n", -1, "number")
)
var clear map[string]func()
func init()
clear = make(map[string]func())
clear["linux"] = func()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
clear["windows"] = func()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
func CallClear()
value, ok := clear[runtime.GOOS]
if ok
value()
else
panic("Panic!")
func random(min int, max int) int
return rand.Intn((max-min) + 1) + min
func main()
flag.Parse()
file, err := os.Open(*csvPath)
if err != nil
log.Println(err)
return
defer file.Close()
csvReader := csv.NewReader(file)
csvData, err := csvReader.ReadAll()
if err != nil
log.Println(err)
return
qaRaw := make(map[string]string, len(csvData))
for _, data := range csvData
qaRaw[data[0]] = data[1]
qaPair := make(map[string]string)
if *num != -1
i := 1
for one, two := range qaRaw
if i <= *num
qaPair[one] = two
i++
else
for one, two := range qaRaw
qaPair[one] = two
done := make(chan bool)
go func()
if *flagNoColor
color.NoColor = true
white := color.New(color.FgWhite)
boldWhite := white.Add(color.Bold)
qNum := 0
for len(qaPair) >= 1
for one, two := range qaPair
rand.Seed(time.Now().UnixNano())
randomNum := random(1, 2)
if *side != -1
randomNum = *side
var userInput string
CallClear()
boldWhite.Printf("n #t%d / %dn", qNum+1, qNum+len(qaPair))
qNum++
if randomNum == 1
boldWhite.Printf("n Qt%s", one)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", two)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
else if randomNum == 2
boldWhite.Printf("n Qt%s", two)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", one)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
CallClear()
done <- true
()
select
case <-done:
fmt.Println("blah blah blah")
Flashcards example data (.csv file)
"hola","hi"
"sÃÂ","yes"
"ÿqué pasa?","what's up?"
"ávamos!","let's go!"
"ásalud!","cheers!; bless you!"
"no","no"
"por favor","please"
"lo siento","I'm sorry"
"buenos dÃÂas","good morning"
"buenas noches","good night"
"gracias","thank you"
"vale","okay"
"hasta luego","see you later"
"adiós","goodbye"
"cómo","how (questions)"
go
This is a simple flashcard app. With flags to control the sides of the flashcards and the number of flashcards in the session.
All the flashcards without any input at the prompt, are deleted from the map. And ones with an input, remain. The idea is that, the user marks the flashcard that they want to revise in case they had forgotten it, later in the session.
The outer loop, loops until the length of the map is >= 1
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"runtime"
"time"
"github.com/fatih/color"
)
var (
flagNoColor = flag.Bool("no-color", false, "disable color output")
csvPath = flag.String("src", "src.csv", "source")
side = flag.Int("s", -1, "side")
num = flag.Int("n", -1, "number")
)
var clear map[string]func()
func init()
clear = make(map[string]func())
clear["linux"] = func()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
clear["windows"] = func()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
func CallClear()
value, ok := clear[runtime.GOOS]
if ok
value()
else
panic("Panic!")
func random(min int, max int) int
return rand.Intn((max-min) + 1) + min
func main()
flag.Parse()
file, err := os.Open(*csvPath)
if err != nil
log.Println(err)
return
defer file.Close()
csvReader := csv.NewReader(file)
csvData, err := csvReader.ReadAll()
if err != nil
log.Println(err)
return
qaRaw := make(map[string]string, len(csvData))
for _, data := range csvData
qaRaw[data[0]] = data[1]
qaPair := make(map[string]string)
if *num != -1
i := 1
for one, two := range qaRaw
if i <= *num
qaPair[one] = two
i++
else
for one, two := range qaRaw
qaPair[one] = two
done := make(chan bool)
go func()
if *flagNoColor
color.NoColor = true
white := color.New(color.FgWhite)
boldWhite := white.Add(color.Bold)
qNum := 0
for len(qaPair) >= 1
for one, two := range qaPair
rand.Seed(time.Now().UnixNano())
randomNum := random(1, 2)
if *side != -1
randomNum = *side
var userInput string
CallClear()
boldWhite.Printf("n #t%d / %dn", qNum+1, qNum+len(qaPair))
qNum++
if randomNum == 1
boldWhite.Printf("n Qt%s", one)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", two)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
else if randomNum == 2
boldWhite.Printf("n Qt%s", two)
fmt.Scanln(&userInput)
boldWhite.Printf("n At%s", one)
fmt.Scanln(&userInput)
if len(userInput) == 0
delete(qaPair, one)
CallClear()
done <- true
()
select
case <-done:
fmt.Println("blah blah blah")
Flashcards example data (.csv file)
"hola","hi"
"sÃÂ","yes"
"ÿqué pasa?","what's up?"
"ávamos!","let's go!"
"ásalud!","cheers!; bless you!"
"no","no"
"por favor","please"
"lo siento","I'm sorry"
"buenos dÃÂas","good morning"
"buenas noches","good night"
"gracias","thank you"
"vale","okay"
"hasta luego","see you later"
"adiós","goodbye"
"cómo","how (questions)"
go
edited May 14 at 5:26
asked May 14 at 5:17
45d7
512
512
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
var clear map[string]func()
To handle OS specific implementations you may consider using Golang tags feature. See it in action:
flashcard_windows.go
// +build windows
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
flashcard_unix.go
// +build linux
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
To build run go build
with -tags windows
or -tags linux
arguments.
Now there is no need call panic()
. go build
will exit if implementation of Clear
function is missing.
See this for more information.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
var clear map[string]func()
To handle OS specific implementations you may consider using Golang tags feature. See it in action:
flashcard_windows.go
// +build windows
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
flashcard_unix.go
// +build linux
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
To build run go build
with -tags windows
or -tags linux
arguments.
Now there is no need call panic()
. go build
will exit if implementation of Clear
function is missing.
See this for more information.
add a comment |Â
up vote
3
down vote
var clear map[string]func()
To handle OS specific implementations you may consider using Golang tags feature. See it in action:
flashcard_windows.go
// +build windows
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
flashcard_unix.go
// +build linux
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
To build run go build
with -tags windows
or -tags linux
arguments.
Now there is no need call panic()
. go build
will exit if implementation of Clear
function is missing.
See this for more information.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
var clear map[string]func()
To handle OS specific implementations you may consider using Golang tags feature. See it in action:
flashcard_windows.go
// +build windows
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
flashcard_unix.go
// +build linux
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
To build run go build
with -tags windows
or -tags linux
arguments.
Now there is no need call panic()
. go build
will exit if implementation of Clear
function is missing.
See this for more information.
var clear map[string]func()
To handle OS specific implementations you may consider using Golang tags feature. See it in action:
flashcard_windows.go
// +build windows
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
flashcard_unix.go
// +build linux
package main
import(
"os"
"os/exec"
)
func Clear()
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
To build run go build
with -tags windows
or -tags linux
arguments.
Now there is no need call panic()
. go build
will exit if implementation of Clear
function is missing.
See this for more information.
edited May 14 at 7:19
answered May 14 at 7:05
sineemore
1,193217
1,193217
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194340%2fflashcard-cli-app-in-golang%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password