go编译错误:未定义的变量
编程新手/甚至更新。在使用小型 go 程序时遇到问题 - 无法编译并出现未定义的变量错误。代码:
package main
import (
"fmt"
"io"
"os"
)
const file = "readfile.txt"
var s string
func lookup(string) (string, string, string) {
artist := s
album := s
year := s
return artist, album, year
}
func enterdisk() (string, string, string) {
var artist string
var album string
var year string
println("enter artist:")
fmt.Scanf("%s", &artist)
println("enter album:")
fmt.Scanf("%s", &album)
println("enter year:")
fmt.Scanf("%s", &year)
return artist, album, year
}
func main() {
println("enter UPC or [manual] to enter information manually:")
fmt.Scanf("%s", &s)
s := s
switch s {
case "manual\n": artist, album, year := enterdisk()
default: artist, album, year := lookup(s)
}
f,_ := os.OpenFile(file, os.O_APPEND|os.O_RDWR, 0666)
io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))
f.Close()
println("wrote data to file")
}
和错误:
catalog.go:49: undefined: artist
catalog.go:49: undefined: album
catalog.go:49: undefined: year
但是,这些变量在代码运行之前不会被定义。此外,“lookup”函数尚未编写,它只是返回传递的内容。我知道 Lookup 和 Enterdisk 函数可以独立工作,但我正在尝试测试 switch 语句。
我尝试在 main 中声明变量,但是出现此错误:
catalog.go:49: artist declared and not used
catalog.go:49: album declared and not used
catalog.go:49: year declared and not used
ps 我已阅读 http: //tip.goneat.org/doc/go_faq.html#unused_variables_and_imports ,我同意如果这只是语义,我仍然想修复它。我只是不知道怎么办!
new to programming / even newer to go. having trouble with a small go program - will not compile with undefined variable errors. the code:
package main
import (
"fmt"
"io"
"os"
)
const file = "readfile.txt"
var s string
func lookup(string) (string, string, string) {
artist := s
album := s
year := s
return artist, album, year
}
func enterdisk() (string, string, string) {
var artist string
var album string
var year string
println("enter artist:")
fmt.Scanf("%s", &artist)
println("enter album:")
fmt.Scanf("%s", &album)
println("enter year:")
fmt.Scanf("%s", &year)
return artist, album, year
}
func main() {
println("enter UPC or [manual] to enter information manually:")
fmt.Scanf("%s", &s)
s := s
switch s {
case "manual\n": artist, album, year := enterdisk()
default: artist, album, year := lookup(s)
}
f,_ := os.OpenFile(file, os.O_APPEND|os.O_RDWR, 0666)
io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))
f.Close()
println("wrote data to file")
}
and the errors:
catalog.go:49: undefined: artist
catalog.go:49: undefined: album
catalog.go:49: undefined: year
however, those variables will not be defined until the code runs. additionally, the "lookup" function is not yet written, which it just returns what it is passed. i know the lookup and enterdisk functions work on their own, but i am trying to test the switch statement.
i have tried declaring the variables in main, however i get this error:
catalog.go:49: artist declared and not used
catalog.go:49: album declared and not used
catalog.go:49: year declared and not used
p.s. i have read http://tip.goneat.org/doc/go_faq.html#unused_variables_and_imports , and i agree that if this is only semantics, i still want to fix it. i just don't know how!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
了解区块和Go 中的声明和范围 。
artist
的短变量声明的范围,<switch
case
和default
case 子句中的 code>album 和year
变量开始和结束在每个子句中(最里面的包含 堵塞)。artist
、album
和year
变量不再存在,并且对WriteString()
语句不可见。相反,写:
因此,
artist
、album
和year
变量不再使用 短变量声明位于 switch case 子句中,因为这会隐藏外部块中的变量声明,它们只是被赋值。Read about blocks and declarations and scope in Go.
The scope of the short variable declarations of the
artist
,album
, andyear
variables in theswitch
case
anddefault
case clauses begins and ends within each clause (the innermost containing block). Theartist
,album
, andyear
variables no longer exist and are not visible to theWriteString()
statement.Instead, write:
Therefore, the
artist
,album
, andyear
variables are no longer declared (and assigned) using short variable declarations inside the switch case clauses because that would hide the variable declarations in the outer block, they are merely assigned.当您有条件地分配变量时,您必须在条件之前声明它们。因此,不要:
...尝试这个:(
即使您设置了默认值,编译器也不喜欢它们没有首先声明。或者它可能不喜欢它们被声明两次,每个条件一次,我不确定)
你看,现在我们首先声明变量,并在 switch 条件内设置它们的值。一般规则是:如果要在 if/switch/for 之外使用变量,请首先声明它们,以使它们在将使用的范围内可访问。
When you have variables being assigned conditionally, you must declare them before the conditions. So instead of:
...try this:
(even though you have set a default, the compiler doesn't like that they are not declared first. Or maybe it doesn't like that they are declared twice, one in each condition, I'm not sure)
You see, now first we declare the variables, and inside the switch condition their values are set. The general rule is: if you are going to use the variables outside of the if/switch/for, declare them first to make them accessible in the scope they will be used.