How to compile Go program consisting of multiple files?
Asked Answered
T

7

120

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 just one file (main.go), everything worked fine.

Now that I took some effort to separate the code, it looks like the compiler is unable to find the stuff that was taken out of main.go and put into these two other files (that reside in the same directory as the main.go). Which results in undefined 'type' errors.

How to compile this program that consists of multiple files?

Tingey answered 31/5, 2012 at 17:35 Comment(0)
J
97

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)

Juline answered 31/5, 2012 at 17:43 Comment(5)
why not get rid of the /go/ and just do myproject/src?Warnerwarning
also, I assume "go install myprog" writes the project source into the $GOPATH location somehow? Is that right? if so, would be nice to have that explanation as part of the answer as wellWarnerwarning
@AlexanderMills Only small projects are using just one technology.Tamelatameless
@DenysSéguret not sure I agree with that, but anyway this is for simple projects, you could just drop the go part - people might assume that the go thing is necessary and it's not. You tell me, which is simpler, the go or the no-go? :)Warnerwarning
I still don't understand why this folder structure?Inveracity
L
71

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.

Lollygag answered 3/6, 2012 at 16:46 Comment(4)
If you don't want to keep adding more and more files, use regex, with: go run *.goWitte
ok thanks, does Go always write only one executable file out? It doesn't keep the executable files separated (like in Java)?Warnerwarning
@AlexanderMills because of windows, i think, use nix. or mingw at least.Goodnight
yeah I was using MacOS, but maybe it's Bash version 3 that is fuckin' up, I should upgrade to Bash 4.Warnerwarning
I
58

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.

Indehiscent answered 14/5, 2020 at 9:25 Comment(4)
I'm not writing a simple program, lets say i have a big project with multiple packages. How do I create an executable for that? Do I have to go to each package and run go build?Leda
Whether you're creating an extensive program or not, provide a single entry point to it and compile your program through that.Indehiscent
All go files need to have the same package.Sap
All Go files in the same package should have the same package declaration except test files.Indehiscent
F
34

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

Fagaceous answered 8/12, 2014 at 18:43 Comment(2)
the output is a go object, not an executable.Dionysiac
@Dionysiac just add chmod +x permission for that file to run itPinchas
P
14

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
Photoelectron answered 26/1, 2019 at 5:26 Comment(0)
P
8

You can use

go build *.go 
go run *.go

both will work also you may use

go build .
go run .
Patricepatrich answered 16/4, 2019 at 4:44 Comment(0)
H
3

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.

Habited answered 25/6, 2019 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.