'"undefined" function declared in another file?
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!"
}
Solution 1:[1]
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
.
Solution 2:[2]
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
.
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
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.
Solution 6:[6]
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.
Solution 7:[7]
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
Solution 8:[8]
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
Solution 9:[9]
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
Solution 10:[10]
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.
Solution 11:[11]
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
Solution 12:[12]
Off topic but still :-)
Before:
func NewEmployee() *Employee {
p := &Employee{}
return p
}
After:
func NewEmployee() *Employee {
return &Employee{}
}
No need to create a new variable. Just return.
Solution 13:[13]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow