Flashcard CLI app in Golang

The name of the pictureThe name of the pictureThe name of the pictureClash 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)"






share|improve this question



























    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)"






    share|improve this question























      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)"






      share|improve this question













      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)"








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 14 at 5:26
























      asked May 14 at 5:17









      45d7

      512




      512




















          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.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer



























              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.






              share|improve this answer

























                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.






                share|improve this answer















                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.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited May 14 at 7:19


























                answered May 14 at 7:05









                sineemore

                1,193217




                1,193217






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?