118

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?

Ashfaq
  • 1,054
  • 8
  • 13
Vitaly
  • 4,358
  • 7
  • 47
  • 59

7 Answers7

96

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)

Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 6
    why not get rid of the /go/ and just do myproject/src? – Alexander Mills Jan 18 '17 at 04:30
  • 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 well – Alexander Mills Jan 18 '17 at 04:32
  • 1
    @AlexanderMills Only small projects are using just one technology. – Denys Séguret Jan 18 '17 at 08:08
  • 3
    @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? :) – Alexander Mills Jan 18 '17 at 08:13
  • 1
    I still don't understand why this folder structure? – Matteo Sep 21 '19 at 08:45
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.

hoijui
  • 3,615
  • 2
  • 33
  • 41
Kissaki
  • 8,810
  • 5
  • 40
  • 42
56

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.

Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
  • 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? – insanely_sin Nov 08 '21 at 02:58
  • 1
    Whether you're creating an extensive program or not, provide a single entry point to it and compile your program through that. – Inanc Gumus Feb 12 '22 at 12:06
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

poopoothegorilla
  • 764
  • 7
  • 13
12

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
iltempo
  • 15,718
  • 8
  • 61
  • 72
alamin
  • 2,377
  • 1
  • 26
  • 31
8

You can use

go build *.go 
go run *.go

both will work also you may use

go build .
go run .
Tushar Saha
  • 1,978
  • 24
  • 29
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.

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85