go编译错误:未定义的变量(go compile errors: undefined variables)

编程入门 行业动态 更新时间:2024-10-25 10:24:34
go编译错误:未定义的变量(go compile errors: undefined variables)

新编程甚至更新。 使用小型程序时遇到问题 - 不会使用未定义的变量错误进行编译。 代码:

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

但是,在代码运行之前,不会定义这些变量。 另外,“查找”功能尚未写入,它只返回传递的内容。 我知道查找和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!

最满意答案

阅读Go中的 块和声明以及范围 。

switch或select语句中的每个子句都充当隐式块。

阻止巢并影响范围。

声明标识符的范围是源文本的范围,其中标识符表示指定的常量,类型,变量,函数或包。

在函数内声明的常量或变量标识符的范围从ConstSpec或VarSpec(ShortVarDecl用于短变量声明)的末尾开始,并在最内层包含块的末尾结束。

switch s { case "manual\n": artist, album, year := enterdisk() default: artist, album, year := lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))

switch case中的artist , album和year变量的short变量声明的范围和default case子句在每个子句(最里面的包含块)内开始和结束。 artist , album和year变量不再存在,并且对WriteString()语句不可见。

相反,写:

var artist, album, year string switch s { case "manual\n": artist, album, year = enterdisk() default: artist, album, year = lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))

与常规变量声明不同,短变量声明可以重新声明变量,前提是它们最初在具有相同类型的同一块中声明,并且至少有一个非空变量是新的。 因此,重新声明只能出现在多变量简短声明中。

因此, artist , album和year变量不再使用switch case子句中的短变量声明声明(和分配),因为这会隐藏外部块中的变量声明,它们仅被分配。

Read about blocks and declarations and scope in Go.

Each clause in a switch or select statement acts as an implicit block.

Blocks nest and influence scoping.

The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.

switch s { case "manual\n": artist, album, year := enterdisk() default: artist, album, year := lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))

The scope of the short variable declarations of the artist, album, and year variables in the switch case and default case clauses begins and ends within each clause (the innermost containing block). The artist, album, and year variables no longer exist and are not visible to the WriteString() statement.

Instead, write:

var artist, album, year string switch s { case "manual\n": artist, album, year = enterdisk() default: artist, album, year = lookup(s) } . . . io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "\n"))

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.

Therefore, the artist, album, and year 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.

更多推荐

本文发布于:2023-08-05 05:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   错误   未定义   compile   undefined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!