卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34563本站已运行393

Golang 程序中命令行参数未正确接受作为参数

golang 程序中命令行参数未正确接受作为参数

php小编百草在Golang程序开发中,有时会遇到命令行参数未正确接受作为参数的问题。这个问题可能导致程序无法正常运行或无法获取正确的输入数据。为了解决这个问题,我们需要仔细检查程序中的命令行参数处理部分,确保参数被正确接收并在程序中得到正确使用。本文将介绍一些常见的错误原因和解决方法,帮助开发者更好地处理命令行参数。

问题内容

我是 golang 新手,正在学习有关使用命令行终端制作简单测验应用程序的在线教程。然而,当我在我的机器上运行代码时,在第一个问题之后,其余的问题成对出现,并且它不再接受我对每个问题的答案。

屏幕截图示例:

程序的流程非常简单 -

  1. 从本地 csv 文件获取输入问题
  2. 打印每个问题并接受用户输入的答案
  3. 维护正确答案的计数并在最后显示它们。

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 %dn", 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 有未解决的评论,并且被屏蔽了很长时间。

卓越飞翔博客
上一篇: Gosseract 未运行
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏