Golang 程序无法正确接收命令行参数作为参数
来源:stackoverflow
时间:2024-02-07 17:06:24 179浏览 收藏
今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《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 程序无法正确接收命令行参数作为参数》文章吧,也可关注golang学习网公众号了解相关技术文章。
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习