gpt4 book ai didi

variables - 去编译错误: undefined variables

转载 作者:IT王子 更新时间:2023-10-29 00:57:14 27 4
gpt4 key购买 nike

编程新手/甚至更新。一个小的 go 程序有问题 - 不会编译带有 undefined variable 错误。代码:

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 和 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

附注我读过http://tip.goneat.org/doc/go_faq.html#unused_variables_and_imports ,我同意,如果这只是语义,我仍然想修复它。我只是不知道怎么办!

最佳答案

了解 blocksdeclarations and scopeGo .

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"))

short variable declarations的范围switch case 中的 artistalbumyear 变量code>default case 子句在每个子句(最里面的包含 block )内开始和结束。 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"))

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.

因此,artistalbumyear 变量不再使用 short variable declarations 声明(和分配)在 switch case 子句中因为这会隐藏外部 block 中的变量声明,所以它们只是被赋值。

关于variables - 去编译错误: undefined variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com