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 at the same directory as the main.go). Which results in "undefined 'type'" errors.
How to compile this program that is consisting of multiple files?
This question is related to
go
build
compiler-errors
Since Go 1.11+, GOPATH is no longer recommended, the new way is using Go Modules.
simple
:Create a directory:
mkdir simple
cd simple
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.
Put all your files in that directory.
Finally, run:
go run .
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.
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
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
go build main.go
go build main.go
go build myproject/adapter
exe file will be created at the directory you are currently at.
You can use
go build *.go
go run *.go
both will work also you may use
go build .
go run .
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.
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
Source: Stackoverflow.com