How to compile Go program consisting of multiple files?

前端 未结 7 1069
野的像风
野的像风 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 09:59

    When you separate code from main.go into for example more.go, you simply pass that file to go build/go run/go install as well.

    So if you previously ran

    go build main.go
    

    you now simply

    go build main.go more.go
    

    As further information:

    go build --help
    

    states:

    If the arguments are a list of .go files, build treats them as a list of source files specifying a single package.


    Notice that go build and go install differ from go run in that the first two state to expect package names as arguments, while the latter expects go files. However, the first two will also accept go files as go install does.

    If you are wondering: build will just build the packages/files, install will produce object and binary files in your GOPATH, and run will compile and run your program.

提交回复
热议问题