How to compile Go program consisting of multiple files?

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

    You could also just run

    go build
    

    in your project folder myproject/go/src/myprog

    Then you can just type

    ./myprog
    

    to run your app

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 10:02

    You can use

    go build *.go 
    go run *.go
    

    both will work also you may use

    go build .
    go run .
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-04 10:09

    Yup! That's very straight forward and that's where the package strategy comes into play. there are three ways to my knowledge. folder structure:

    GOPATH/src/ github.com/ abc/ myproject/ adapter/ main.go pkg1 pkg2 warning: adapter can contain package main only and sun directories

    1. navigate to "adapter" folder. Run:
        go build main.go
    
    1. navigate to "adapter" folder. Run:
        go build main.go
    
    1. navigate to GOPATH/src recognize relative path to package main, here "myproject/adapter". Run:
        go build myproject/adapter
    

    exe file will be created at the directory you are currently at.

    0 讨论(0)
  • 2020-12-04 10:10

    Since Go 1.11+, GOPATH is no longer recommended, the new way is using Go Modules.

    Say you're writing a program called simple:

    1. Create a directory:

      mkdir simple
      cd simple
      
    2. Create a new module:

      go mod init github.com/username/simple
      # Here, the module name is: github.com/username/simple.
      # You're free to choose any module name.
      # It doesn't matter as long as it's unique.
      # It's better to be a URL: so it can be go-gettable.
      
    3. Put all your files in that directory.

    4. Finally, run:

      go run .
      
    5. Alternatively, you can create an executable program by building it:

      go build .
      
      # then:
      ./simple     # if you're on xnix
      
      # or, just:
      simple       # if you're on Windows
      

    For more information, you may read this.

    Go has included support for versioned modules as proposed here since 1.11. The initial prototype vgo was announced in February 2018. In July 2018, versioned modules landed in the main Go repository. In Go 1.14, module support is considered ready for production use, and all users are encouraged to migrate to modules from other dependency management systems.

    0 讨论(0)
提交回复
热议问题