3

I have a project structure that looks like so:

enter image description here I was planning on just using a shell script (build.sh) to set GOPATH and build the project.

I am not sure how to build a Golang project properly, my short term goal is to just to build the packages in the src directory and put the binaries into the bin directory.

I am not sure what go build command can do that, I have tried a few things.

So first my question is - is this a reasonable project structure and second, how can I compile my src directory to bin?

What I have gives me the following error:

can't load package: package .: no buildable Go source files in /home/oleg/WebstormProjects/oresoftware/stack-server

So I believe I need to tell Go to look in the src directory, because Go is only looking for .go files in the project root, but I am not sure how to do that.

If it matters, the main.go file has a package name of "main".

7
  • 1
    Try go build -o ./bin/main ./src/main.go Commented Jan 18, 2017 at 4:10
  • thanks I will try that - can you also explain what to do if there is more than one .go file in the src directory? Commented Jan 18, 2017 at 4:14
  • would it be: go build -o ./bin/main ./src/**/* Commented Jan 18, 2017 at 4:15
  • 1
    This should be enough: go build -o ./bin/main ./src. I use a similar pattern (I use the same code base for multiple apps, each app has different module composition). Commented Jan 18, 2017 at 4:31
  • 1
    Currently (Go <= 1.7), yes. But we will have a working plugin system built-in in Go 1.8 which is due next month. Commented Jan 18, 2017 at 4:39

1 Answer 1

3
GOPATH=$PROJECT_DIR && cd $PROJECT_DIR && go install main

Also move your main.go file into src/main/main.go.

This will produce a bin/main executable.

If you have multiple executables you wanna build, you have to put each main.go file into a separate folder/package. The name of the generated executable is taken from the directory name the file is inside. The package name of the main.go files must always be main if it should create a binary.

To compile multiple executables you have to use:

GOPATH=$PROJECT_DIR && cd $PROJECT_DIR && go install ...

The ... matches all folders/packages.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.