


Command line parameters in Golang program are not correctly accepted as parameters
#php editor Baicao sometimes encounters the problem that command line parameters are not correctly accepted as parameters when developing Golang programs. This issue may cause the program to not run properly or to obtain correct input data. In order to solve this problem, we need to carefully check the command line parameter processing part of the program to ensure that the parameters are received correctly and used correctly in the program. This article will introduce some common error causes and solutions to help developers better handle command line parameters.
Question content
I am new to golang and following an online tutorial on making a simple quiz application using command line terminal. However, when I run the code on my machine, after the first question, the remaining questions come in pairs, and it no longer accepts my answer for each question.
Screenshot example:
The process of the program is very simple -
- Issue getting input from local csv file
- Print each question and accept user-entered answers
- Maintain a count of correct answers and display them at the end.
csv file is also very short -
70+22,92 63+67,130 91+72,163 74+61,135 81+6,87
This is the complete program -
package main import ( "encoding/csv" "flag" "fmt" "os" "time" ) func main() { // 1. input the name of the file fName := flag.String("f", "quiz.csv", "path of csv file") // 2. set the duration of timer timer := flag.Int("t", 30, "timer for the quiz") flag.Parse() // 3. pull the problems from the file (calling our problem puller) problems, err := problemPuller(*fName) // 4. handle the error if err != nil { exit(fmt.Sprintf("something went wrong: %s", err.Error())) } // 5. create a variable to count our correct answers correctAns := 0 // 6. using the duration of the timer, we want to initialize the timer tObj := time.NewTimer(time.Duration(*timer) * time.Second) ansC := make(chan string) // 7. loop through the problems, print the questions, we'll accept the answers problemLoop: for i, p := range problems { var answer string fmt.Printf("Problem %d: %s =", i+1, p.question) go func() { fmt.Scanf("%s", &answer) ansC <- answer }() select { case <- tObj.C: fmt.Println() break problemLoop case iAns := <- ansC: if iAns == p.answer { correctAns++ } if i == len(problems)-1 { close(ansC) } } } // 8. calculate and print out the result fmt.Printf("Your result is %d out of %d\n", correctAns, len(problems)) fmt.Printf("Press enter to exit") <- ansC } func problemPuller(fileName string) ([]problem, error) { // read all the problems from the quiz.csv // 1. open the file if fObj, err := os.Open(fileName); err == nil { // 2. we will create new reader csvR := csv.NewReader(fObj) // 3. it will need to read the file if cLines, err := csvR.ReadAll(); err == nil { // 4. call the parseProblem function return parseProblem(cLines), nil } else { return nil, fmt.Errorf("error in reading data in csv from %s file; %s", fileName, err.Error()) } } else { return nil, fmt.Errorf("error in opening the file %s file; %s", fileName, err.Error()) } } func parseProblem(lines [][]string) []problem { // go over the lines and parse them based on the problem struct r := make([] problem, len(lines)) for i := 0; i < len(lines); i++ { r[i] = problem { question: lines[i][0], answer: lines[i][1], } } return r } type problem struct { question string answer string } func exit(msg string) { fmt.Println(msg) os.Exit(1) }
I tried every line of code but can't solve it. Can someone point out what I'm doing wrong?
Workaround
I can reproduce the issue on windows (running in Command Prompt
). But on linux there is no problem.
The following changes will resolve the issue:
go func() { - fmt.Scanf("%s", &answer) + fmt.Scanln(&answer) ansC <- answer }()
This is a known issue, reported as fmt: scanf works differently on windows and linux #23562. There is also a pending fix. Unfortunately, cl has unresolved comments and has been blocked for a long time.
The above is the detailed content of Command line parameters in Golang program are not correctly accepted as parameters. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...
