Golang 未定义的变量

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

Golang undefined variable

问题

我有两个go文件:

/Users/username/go/src/Test/src/main/Test.go

package main

import "fmt"

func main() {
    fmt.Printf(SomeVar)
}

和文件/Users/username/go/src/Test/src/main/someFile.go

package main

const SomeVar = "someFile"

然而,我一直得到编译器错误:

/Users/username/go/src/Test/src/main/Test.go:6: undefined: SomeVar

有人可以解释一下为什么SomeVar被标记为未定义吗?

英文:

I have 2 go files:

/Users/username/go/src/Test/src/main/Test.go

package main

import "fmt"

func main() {
    fmt.Printf(SomeVar)
}

and file /Users/username/go/src/Test/src/main/someFile.go

package main

const SomeVar = "someFile"

However I am constantly getting compiler error:

>/Users/username/go/src/Test/src/main/Test.go:6: undefined: SomeVar

Can someone explain to me why is SomeVar labeled as undefined?

答案1

得分: 16

尝试运行以下命令:

go run Test.go someFile.go
英文:

Try

go run Test.go someFile.go

答案2

得分: 11

引用

> 我认为你对go工具的工作原理有误解。你可以在一个目录中执行"go build"命令,它将构建整个包(一个包被定义为目录中的所有.go文件)。go install、go test等命令也是如此。只有go run命令需要你指定具体的文件...它实际上只适用于非常小的程序,通常只需要一个文件。

所以执行以下命令:

go build && ./program_name

另请参阅

英文:

Quote:

> I think you're misunderstanding how the go tool works. You can do "go
> build" in a directory, and it'll build the whole package (a package is
> defined as all .go files in a directory). Same for go install, go
> test, etc. Go run is the only one that requires you to specify
> specific files... it's really only meant to be used on very small
> programs, which generally only need a single file.

So do:

go build && ./program_name

See also

答案3

得分: 9

你的代码是正确的:

  • someFile.goTest.go 属于同一个包(main
  • SomeVar 是一个在顶层声明的 const,因此它具有包级作用域,即 main 包级作用域
  • 因此,SomeVar 是可见的,并且可以在两个文件中访问

(如果你需要复习 Go 中的作用域,请参考语言规范 - 声明和作用域)。

那么为什么会出现“undefined”错误呢?

你可能执行了 go build Test.gogo run Test.go,如果从 /Users/username/go/src/Test/src/main 目录启动,会产生以下输出:

# command-line-arguments
./Test.go:6: undefined: SomeVar

你可以在这里找到原因:命令 go

如果你使用一系列 .go 文件来执行 go buildgo run,它会将它们视为指定单个包的源文件列表,即它认为在 main 包中没有其他代码片段,因此会出现错误。
解决方法是包括所有所需的 .go 文件:

go build Test.go someFile.go

go run Test.go someFile.go

go build 也可以不带参数工作,构建包中找到的所有文件:

go build

注意1:上述命令是针对本地包的,因此必须从 /Users/username/go/src/Test/src/main 目录启动。

注意2:尽管其他答案已经提供了有效的解决方案,但我决定在这里添加一些更多的细节,以帮助社区,因为这是在开始使用 Go 时常见的问题 Golang 未定义的变量

英文:

You code is correct:

  • someFile.go and Test.go belong to the same package (main)
  • SomeVar is a const declared at top level, so it has a package block scope, namely the main package block scope
  • as a consequence, SomeVar is visible and can be accessed in both files

(if you need to review scoping in Go, please refer to the Language Specification - Declarations and Scope).

Why do you get an undefined error then?

You probably launched go build Test.go or go run Test.go, both producing the following output, if launched from /Users/username/go/src/Test/src/main:

# command-line-arguments
./Test.go:6: undefined: SomeVar

You can find the reason here: Command go

If you launch go build or go run with a list of .go files, it treats them as a list of source files specifying a single package, i.e., it thinks there are no other pieces of code in the main package, hence the error.
The solution is including all the required .go files:

go build Test.go someFile.go

go run Test.go someFile.go

go build will also work with no arguments, building all the files it finds in the package as a result:

go build

Note 1: the above commands refer to the local package and, as such, must be launched from the /Users/username/go/src/Test/src/main directory

Note 2: though other answers already proposed valid solutions, I decided to add a few more details here to help the community, since this is a common question when you start working with Go Golang 未定义的变量

答案4

得分: 2

由于我在遇到类似问题时登陆到这个页面,我将把这个答案和已经提到的内容一起包含在内。如果你正在使用 go run,你可以使用以下命令:

go run . 或者 go run .*

这个问题也在这个问题中得到了回答:

https://stackoverflow.com/questions/23695448/how-to-run-all-go-files-within-current-directory-through-the-command-line-mult

英文:

Due to landing on this page when I had a similar question, I'll include this answer alongside of what has already been stated. If you are using go run you may use:

go run . or go run .*

This question was also answered on this question

https://stackoverflow.com/questions/23695448/how-to-run-all-go-files-within-current-directory-through-the-command-line-mult

huangapple
  • 本文由 发表于 2014年7月5日 10:17:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/24582384.html
匿名

发表评论

匿名网友

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

确定