为什么这个给我一个未定义的错误?

huangapple go评论67阅读模式
英文:

Why is this giving me an undefined error?

问题

我正在使用Go语言编写一个简单的CSV文件解析器,但是在以下代码中无法找到为什么会出现"undefined: csvfile"和"undefined: err"的错误。从所有的示例中看起来,代码是正确的。

var source string
flag.StringVar(&source, "file", "test.csv", "the file to parse")
flag.Parse()
csvfile, err := os.Open(source)

这段代码中的问题在于变量csvfileerr没有使用:=进行声明和赋值。正确的写法应该是:

var source string
flag.StringVar(&source, "file", "test.csv", "the file to parse")
flag.Parse()
csvfile, err := os.Open(source)

希望对你有帮助!

英文:

I am writing a simple csv file parser in go and cannot find out why I get "undefined: csvfile" and "undefined: err" with the following code. From all of the examples it appears to be correct.

var source string
flag.StringVar(&source, "file", "test.csv", "the file to parse")
flag.Parse()
csvfile, err = os.Open(source)

答案1

得分: 1

使用:=而不是=来创建新变量:

csvfile, err := os.Open(source)
英文:

Use :=, not =, to create new variables:

csvfile, err := os.Open(source)

huangapple
  • 本文由 发表于 2015年4月12日 04:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/29582957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定