Golang 程序中命令行参数未正确接受作为参数
php小编百草在Golang程序开发中,有时会遇到命令行参数未正确接受作为参数的问题。这个问题可能导致程序无法正常运行或无法获取正确的输入数据。为了解决这个问题,我们需要仔细检查程序中的命令行参数处理部分,确保参数被正确接收并在程序中得到正确使用。本文将介绍一些常见的错误原因和解决方法,帮助开发者更好地处理命令行参数。
问题内容
我是 golang 新手,正在学习有关使用命令行终端制作简单测验应用程序的在线教程。然而,当我在我的机器上运行代码时,在第一个问题之后,其余的问题成对出现,并且它不再接受我对每个问题的答案。
屏幕截图示例:
程序的流程非常简单 -
- 从本地 csv 文件获取输入问题
- 打印每个问题并接受用户输入的答案
- 维护正确答案的计数并在最后显示它们。
csv 文件也很短 -
70+22,92 63+67,130 91+72,163 74+61,135 81+6,87
这是完整的程序 -
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) }
我尝试过每一行代码,但无法解决它。有人可以指出我做错了什么吗?
解决方法
我可以在 windows 上重现该问题(在 命令提示符
中运行)。但在 linux 上没有问题。
以下更改将解决该问题:
go func() { - fmt.Scanf("%s", &answer) + fmt.Scanln(&answer) ansC <- answer }()
这是一个已知问题,报告为fmt:scanf 在 windows 和 linux 上的工作方式不同#23562.并且还有一个待修复。不幸的是,cl 有未解决的评论,并且被屏蔽了很长时间。
以上是Golang 程序中命令行参数未正确接受作为参数的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

OpenSSL,作为广泛应用于安全通信的开源库,提供了加密算法、密钥和证书管理等功能。然而,其历史版本中存在一些已知安全漏洞,其中一些危害极大。本文将重点介绍Debian系统中OpenSSL的常见漏洞及应对措施。DebianOpenSSL已知漏洞:OpenSSL曾出现过多个严重漏洞,例如:心脏出血漏洞(CVE-2014-0160):该漏洞影响OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻击者可利用此漏洞未经授权读取服务器上的敏感信息,包括加密密钥等。

本文演示了创建模拟和存根进行单元测试。 它强调使用接口,提供模拟实现的示例,并讨论最佳实践,例如保持模拟集中并使用断言库。 文章

本文探讨了GO的仿制药自定义类型约束。 它详细介绍了界面如何定义通用功能的最低类型要求,从而改善了类型的安全性和代码可重复使用性。 本文还讨论了局限性和最佳实践

本文讨论了GO的反思软件包,用于运行时操作代码,对序列化,通用编程等有益。它警告性能成本,例如较慢的执行和更高的内存使用,建议明智的使用和最佳

本文讨论了GO中使用表驱动的测试,该方法使用测试用例表来测试具有多个输入和结果的功能。它突出了诸如提高的可读性,降低重复,可伸缩性,一致性和A

本文使用跟踪工具探讨了GO应用程序执行流。 它讨论了手册和自动仪器技术,比较诸如Jaeger,Zipkin和Opentelemetry之类的工具,并突出显示有效的数据可视化
