My project structure is like this.
packagetest/
main.go
lib.go
In main.go
, I have this code.
package m
Run all the file then able to call
go run *.go
or
go run main.go lib.go
If using multiple files for Go main pkg, instead of single file path as go run main.go, go require main pkg dir in the run or build command, like:
#from outside packagetest dir
go run ./packagetest/
go build ./packagetest/
#from within packagetest:
go run ./
go build ./
This problem will happen when go cannot find your other file (like lib.go
in this example).
If you use JetBrains Goland , you should change your "Run kind" to "Directory". Goland will help you to do these:
go build .
If you use JetBrains Goland , you can contrast the difference between "Run kind".
When i select "File", and then run, you will see the message on the console, like this:
GOROOT=C:\Go #gosetup
GOPATH=C:\Users\hgs\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\hgs\AppData\Local\Temp\___go_build_select_go__4_.exe "C:/Users/hgs/Google Drive/My Maps/computer/2.programming_lang/Golang/source-code-refer/basic/select.go" #gosetup
# command-line-arguments
.\select.go:14:16: undefined: Add
When you select "Directory" and select your directory, and then run, you will see something like this:
GOROOT=C:\Go #gosetup
GOPATH=C:\Users\hgs\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\hgs\AppData\Local\Temp\___go_build_basic_.exe . #gosetup
C:\Users\hgs\AppData\Local\Temp\___go_build_basic_.exe #gosetup
...