英文:
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
英文:
> 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
答案3
得分: 9
你的代码是正确的:
someFile.go
和Test.go
属于同一个包(main
)SomeVar
是一个在顶层声明的const
,因此它具有包级作用域,即main
包级作用域- 因此,
SomeVar
是可见的,并且可以在两个文件中访问
(如果你需要复习 Go 中的作用域,请参考语言规范 - 声明和作用域)。
那么为什么会出现“undefined”错误呢?
你可能执行了 go build Test.go
或 go run Test.go
,如果从 /Users/username/go/src/Test/src/main
目录启动,会产生以下输出:
# command-line-arguments
./Test.go:6: undefined: SomeVar
你可以在这里找到原因:命令 go
如果你使用一系列 .go
文件来执行 go build
或 go 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 时常见的问题
英文:
You code is correct:
someFile.go
andTest.go
belong to the same package (main
)SomeVar
is aconst
declared at top level, so it has a package block scope, namely themain
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
答案4
得分: 2
由于我在遇到类似问题时登陆到这个页面,我将把这个答案和已经提到的内容一起包含在内。如果你正在使用 go run,你可以使用以下命令:
go run .
或者 go run .*
这个问题也在这个问题中得到了回答:
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论