英文:
"undefined" function declared in another file?
问题
我正在尝试编写一个基本的Go程序,该程序调用同一包中不同文件的函数。然而,它返回以下错误:
undefined: NewEmployee
以下是源代码:
main.go
:
package main
func main() {
emp := NewEmployee()
}
employee.go
:
package main
type Employee struct {
name string
age int
}
func NewEmployee() *Employee {
p := &Employee{}
return p
}
func PrintEmployee(p *Employee) string {
return "Hello world!"
}
英文:
I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns:
undefined: NewEmployee
Here is the source code:
main.go
:
package main
func main() {
emp := NewEmployee()
}
employee.go
:
package main
type Employee struct {
name string
age int
}
func NewEmployee() *Employee {
p := &Employee{}
return p
}
func PrintEmployee (p *Employee) {
return "Hello world!"
}
答案1
得分: 275
请阅读《如何编写 Go 代码》(http://golang.org/doc/code.html)。
在包目录中使用 go build
或 go install
,或者为包提供导入路径。不要在 build
或 install
中使用文件参数。
虽然你可以在 go run
中使用文件参数,但通常应该构建一个包,使用 go run .
,尽管你几乎总是应该使用 go install
或 go build
。
英文:
Please read "How to Write Go Code".
Use go build
or go install
within the package directory, or supply an import path for the package. Do not use file arguments for build
or install
.
While you can use file arguments for go run
, you should build a package instead, usually with go run .
, though you should almost always use go install
, or go build
.
答案2
得分: 124
我在GoLand(即Go的Intellij IDEA)中遇到了同样的问题,并找到了解决方案。你需要将“运行类型”从“文件”更改为“包”或“目录”。如果你进入“运行/编辑配置”,可以从下拉菜单中选择。
例如:对于包~/go/src/a_package
,使用包路径
为a_package
,目录
为~/go/src/a_package
,并将运行类型
设置为包
或目录
。
英文:
I just had the same problem in GoLand (which is Intellij IDEA for Go) and worked out a solution. You need to change the Run kind
from File
to Package
or Directory
. You can choose this from a drop-down if you go into Run/Edit
Configurations.
Eg: for package ~/go/src/a_package
, use a Package path
of a_package
and a Directory
of ~/go/src/a_package
and Run kind
of Package
or Directory
.
答案3
得分: 52
如果你正在使用 go run
,请执行 go run *.go
。它会自动查找当前工作目录中的所有go文件,编译并运行你的主函数。
英文:
If you're using go run
, do go run *.go
. It will automatically find all go files in the current working directory, compile and then run your main function.
答案4
得分: 18
你可以尝试以下方法之一:
方法1:
- 假设你的项目名称是
MyProject
- 进入项目路径,运行
go build
- 这将创建一个可执行文件,文件名与你的项目名称相同("MyProject")
- 然后使用
./MyProject
运行可执行文件
你也可以通过输入go build && ./MyProject
来同时执行这两个步骤。package main
的Go文件会被编译为可执行文件。
方法2:
- 只需运行
go run *.go
。它不会创建任何可执行文件,但会直接运行代码。
英文:
You can try one of the following:
Method 1:
- Assume that your project name is
MyProject
- Go to your path, run
go build
- It will create an executable file as your project name ("MyProject")
- Then run the executable using
./MyProject
You can do both steps at once by typing go build && ./MyProject
. Go files of the package main
are compiled to an executable.
Method 2:
- Just run
go run *.go
. It won't create any executable but it runs.
答案5
得分: 17
go run .
将运行你的所有文件。入口点是函数 main()
,它必须是 main
包中唯一的。
另一个选项是使用 go build
构建二进制文件,然后运行它。
英文:
go run .
will run all of your files. The entry point is the function main()
which has to be unique to the main
package.
Another option is to build the binary with go build
and run it.
答案6
得分: 5
如果你想从另一个Go文件中调用一个函数,并且你正在使用Goland,那么请在运行菜单中找到“编辑配置”选项,并将运行类型从“文件”更改为“目录”。这将清除所有错误,并允许你从其他Go文件中调用函数。
英文:
If you want to call a function from another go file and you are using Goland, then find the option 'Edit configuration' from the Run menu and change the run kind from File to Directory. It clears all the errors and allows you to call functions from other go files.
答案7
得分: 4
你现在应该使用go modules,如果你没有遵循如何编写Go代码。
使用go module,你不必将代码放在$GOPATH/src中,它可以存在于任何其他位置。
你可以将代码移动到不同的目录,比如/employee,在employee目录下初始化go module即可:
go mod init example.com/employee
英文:
you should use go modules now, if you are not following How to write go code
With go module you don't have to put the code in the $GOPATH/src. it can live in any other location as well.
You can move the code to different directory like /employee, To make it work Just under employee directory initialise the go module
go mod init example.com/employee
答案8
得分: 2
我在我的一个Go文件中遇到了一个讨厌的import "C"
。
英文:
I had a nasty import "C"
in one of my go files.
答案9
得分: 1
我在使用Go11
时遇到了同样的问题,只是想分享一下我是如何解决它的,以帮助其他人遇到同样问题的情况。
我的Go项目位于$GOPATH
之外,所以我必须打开GO111MODULE=on
选项,如果不打开这个选项,就会出现这个问题;即使你尝试构建或测试整个package
或directory
,也无法解决,除非打开GO111MODULE=on
。
英文:
I ran into the same issue with Go11
, just wanted to share how I did solve it for helping others just in case they run into the same issue.
I had my Go project outside $GOPATH
, so I had to turned on GO111MODULE=on
without this option turned on, it will give you this issue; even if you you try to build or test the whole package
or directory
it won't be solved without GO111MODULE=on
答案10
得分: 1
虽然这并没有直接解决提问者的具体问题,但我想提供一下我遇到的“undefined”错误的解决方案:具有未定义方法的文件具有一个构建约束(构建标签)。
更具体地说,我在一个被部署的二进制应用程序中使用的实用函数文件中意外地包含了一个构建约束,以便从部署的应用程序二进制文件中删除测试文件。因此,在提问者的示例中,如果employee.go
具有构建约束,他们的go build
命令需要包含一个与约束匹配的-tags
标志,以便包含该文件。
有关更多信息,请阅读这篇博文:
https://dave.cheney.net/tag/build-constraints
英文:
While this doesn't directly address the OP's specific issue, I thought I'd chime in with the solution to my "undefined" error: the file with the undefined method had a build constraint (build tag).
More specifically, I accidentally included a build constraint to remove testing files from my deployed application binary in a utility function file used by my deployed binary. So in the OP's example - if employee.go
had a build constraints their go build
command would need to include a -tags
flag matching the constraint in in order to have the file included.
For more info read this blog post:
https://dave.cheney.net/tag/build-constraints
答案11
得分: 1
这可能需要一些时间来为自己的 MRE 进行深入研究,所以希望尽管简洁,但它能帮助到其他人:
如果你的函数/结构体定义在一个包含 import "C"
的文件中,也会出现这种情况。如果你的 go 环境中设置了 CGO_ENABLED=0
,那么这个导入语句将会被静默忽略,导致你无法在这两个文件的包之间共享。
英文:
Took a while to drill down my own MRE for this, so hopefully it will help others, despite being brief:
This can also occur if your functions / structs are defined in a file that has import "C"
, which will be silently ignored if CGO_ENABLED=0
is in your go environment, leading to you staring at a two file package that somehow is unable to share between themselves.
答案12
得分: 0
在GoLand中,
- 右键单击一个目录
- GoLand将为您提供构建并运行的选项
- 它还将为您创建一个运行配置草稿
- 您可以使用右上角的下拉菜单选项进行保存
如果您右键单击一个文件,它会显示运行、调试、测试该文件的命令。
如果您右键单击一个目录,对于该“包”来说,情况将是相同的,期望目录中有一个main.go文件。
英文:
In GoLand,
- right click a directory
- GoLand will give you the option for build it and run it
- it will also create a run configuration draft for you
- you can save with an option of the upper right dropdown
If you right clic a file, it shows you commands to run, debug, test that file.
If you right clic a directory, it will be the same for that "package", expecting a main.go file inside the directory
答案13
得分: 0
我在使用Goland时也遇到了这个问题。
需要作为一个包运行。
以下两种方法可以解决。
1
2
英文:
I also encountered this problem when using goland.
Need to run as a package
The following two ways can be solved.
1
2
答案14
得分: -2
如果您的源文件夹结构为/go/src/blog(假设您的源文件夹名称为blog)。
- cd /go/src/blog ...(进入包含您的包的文件夹)
- go install
- blog
这样可以同时运行所有文件,而不需要手动列出文件或在命令行上执行某个方法。
英文:
If your source folder is structured /go/src/blog (assuming the name of your source folder is blog).
- cd /go/src/blog ... (cd inside the folder that has your package)
- go install
- blog
That should run all of your files at the same time, instead of you having to list the files manually or "bashing" a method on the command line.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论