go编译错误:未定义的变量

发布于 2024-12-03 10:29:50 字数 1692 浏览 0 评论 0原文

编程新手/甚至更新。在使用小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-12-10 10:29:50

了解区块Go 中的声明和范围 。

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

阻止嵌套并影响范围。

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

在 a 内声明的常量或变量标识符的范围
函数从 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")) 

artist短变量声明的范围,< switch casedefault case 子句中的 code>album 和 year 变量开始和结束在每个子句中(最里面的包含 堵塞)。 artistalbumyear 变量不再存在,并且对 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"))

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

因此,artistalbumyear 变量不再使用 短变量声明位于 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.

冷默言语 2024-12-10 10:29:50

当您有条件地分配变量时,您必须在条件之前声明它们。因此,不要:

switch s { 
    case "manual\n": artist, album, year := enterdisk()
    default: artist, album, year := lookup(s)
}

...尝试这个:(

var artist, album, year string

switch s { 
    case "manual\n": artist, album, year = enterdisk()
    default: artist, album, year = lookup(s)
}

即使您设置了默认值,编译器也不喜欢它们没有首先声明。或者它可能不喜欢它们被声明两次,每个条件一次,我不确定)

你看,现在我们首先声明变量,并在 switch 条件内设置它们的值。一般规则是:如果要在 if/switch/for 之外使用变量,请首先声明它们,以使它们在将使用的范围内可访问。

When you have variables being assigned conditionally, you must declare them before the conditions. So instead of:

switch s { 
    case "manual\n": artist, album, year := enterdisk()
    default: artist, album, year := lookup(s)
}

...try this:

var artist, album, year string

switch s { 
    case "manual\n": artist, album, year = enterdisk()
    default: artist, album, year = lookup(s)
}

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文