"undefined" function declared in another file?

GoUndefinedFunc

Go Problem Overview


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!"
}

Go Solutions


Solution 1 - Go

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

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

Solution 4 - 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.

Solution 5 - Go

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

Solution 7 - Go

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

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

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

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

Solution 12 - Go

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

If your source folder is structured /go/src/blog (assuming the name of your source folder is blog).

  1. cd /go/src/blog ... (cd inside the folder that has your package)
  2. go install
  3. 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuan MView Question on Stackoverflow
Solution 1 - GoJimBView Answer on Stackoverflow
Solution 2 - GoAndrew W. PhillipsView Answer on Stackoverflow
Solution 3 - GoPaul Razvan BergView Answer on Stackoverflow
Solution 4 - Gocaldera.sacView Answer on Stackoverflow
Solution 5 - GoMiguel ReyesView Answer on Stackoverflow
Solution 6 - GoPuneet TokhiView Answer on Stackoverflow
Solution 7 - GoJeetsangView Answer on Stackoverflow
Solution 8 - GoMuhammad SolimanView Answer on Stackoverflow
Solution 9 - GodylankbView Answer on Stackoverflow
Solution 10 - GoCireoView Answer on Stackoverflow
Solution 11 - Go1P0View Answer on Stackoverflow
Solution 12 - GoIvan PirogView Answer on Stackoverflow
Solution 13 - GoDruhin BalaView Answer on Stackoverflow