How to compile Go program consisting of multiple files?

前端 未结 7 1028
野的像风
野的像风 2020-12-04 09:15

I have a small program that consists of three files, all belonging to the same package (main), but when I do \"go build main.go\" the build doesn\'t succeed. When it was jus

相关标签:
7条回答
  • 2020-12-04 10:10

    It depends on your project structure. But most straightforward is:

    go build ./... -o ./myproject
    

    then run ./myproject.

    Suppose your project structure looks like this

    - hello
    |- main.go
    

    then you just go to the project directory and run

    go build -o ./myproject
    

    then run ./myproject on shell.

    or

    # most easiest; builds and run simultaneously
    go run main.go
    

    suppose your main file is nested into a sub-directory like a cmd

    - hello
    |- cmd
     |- main.go
    

    then you will run

    go run cmd/main.go
    
    0 讨论(0)
提交回复
热议问题