Function in same package undefined

前端 未结 9 1945
太阳男子
太阳男子 2020-12-28 12:17

My project structure is like this.

packagetest/
    main.go
    lib.go

In main.go, I have this code.

package m         


        
相关标签:
9条回答
  • 2020-12-28 12:58

    Run all the file then able to call

    go run *.go
    

    or

    go run main.go lib.go
    
    0 讨论(0)
  • 2020-12-28 13:01

    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 ./
    
    0 讨论(0)
  • 2020-12-28 13:04

    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 .
    • run this execution file.

    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
    ...
    
    0 讨论(0)
提交回复
热议问题