Golang "undefined" function declared in another file?

I am trying to write a basic go program that calls a function from another file, but is 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!" } 
+110
undefined go func
Jan 26 '15 at 15:27
source share
7 answers

Please read "How to write a transition code . "

Do not use /src in GOPATH . Packages are located in $GOPATH/src .

For build or install you need to have your files in the package directory.

For go run you need to specify all the files as an argument:

 go run main.go employee.go 

But you should almost always use go install , or go build (and preferably the first, since go build is confusing when working with non-core packages)

+142
Jan 26 '15 at 15:46
source share

I had the same problem in GoLand and I found a solution. You need to change Run kind from File to Package or Directory . You can select this from the drop-down menu if you go to Run/Edit Configurations.

For a ~/go/src/a_package Package path is a_package and the Directory is ~/go/src/a_package . You can choose Run kind which you like.

+42
Jul 05 '17 at 6:01
source share

If you use go run , go run *.go . go run *.go . It will automatically find all go files in the current working directory, compile and then run your main function.

+6
May 29 '18 at 21:31
source share

You can try one of the following.

Method 01 : Assume Your Project MyProject

  • go to your path, type go build and press enter.
  • it will create an executable as the name of your project ("MyProject")
  • then type in the terminal type ./MyProject and press Enter.

you can do both at once by typing go build &&./MyProject . it will run your project correctly with all go files.

Method 02

just type go run *.go and hit the input. this will execute all your go files .

Hope this helps someone.

+5
Dec 02 '18 at 13:34
source share

If you want to call a function from another go file and use Goland, find the "Edit configuration" option in the "Run" menu and change the startup view from "File to directory". It clears all errors and allows you to call functions from other go files.

+4
Jun 01 '18 at 3:25
source share

If your source folder is structured / go / src / blog (it is assumed that the name of your source folder is a blog).

  • cd / go / src / blog ... (cd inside the folder with your package)
  • go install
  • The blog

This should run all your files at once, instead of manually specifying the files or β€œbeating” the method on the command line.

0
Nov 30 '17 at 20:22
source share

I ran into the same problem with Go11 , just wanted to share how I solved it to help others in case they run into the same problem.

I had a Go project outside of $GOPATH , so I had to turn on GO111MODULE=on if this option is not GO111MODULE=on , this will give you this problem; even if you try to build or test the entire package or directory this will not be resolved without GO111MODULE=on

0
May 14 '19 at 6:46
source share



All Articles