How to compile Go program consisting of multiple files?

前端 未结 7 1063
野的像风
野的像风 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:05

    New Way (Recommended):

    Please take a look at this answer.

    Old Way:

    Supposing you're writing a program called myprog :

    Put all your files in a directory like this

    myproject/go/src/myprog/xxx.go
    

    Then add myproject/go to GOPATH

    And run

    go install myprog
    

    This way you'll be able to add other packages and programs in myproject/go/src if you want.

    Reference : http://golang.org/doc/code.html

    (this doc is always missed by newcomers, and often ill-understood at first. It should receive the greatest attention of the Go team IMO)

提交回复
热议问题