0

Here is the directory structure of my project (~/go/src/bitbucket.org/a/b):

├── cmd
│   ├── c
│   │   └── main.go
│   └── d
│       └── main.go
├── config
│   ├── config.go
│   ├── default.json
│   └── development.json
├── go.mod
├── go.sum
├── log
│   └── log.go
├── main.go

I need to compile 2 binaries (one for each module in cmd/).

I have tried running GO111MODULE=on go build ./cmd/c from project root (~/go/src/bitbucket.org/a/b). It silently finishes without doing anything.

I also tried running GO111MODULE=on go build -o test ./cmd/c. It created 29kb test file. When i add execution rights to it and run, it finishes with error:

./test: 2: ./test: Syntax error: newline unexpected

I have tried using go 1.12.5 and go 1.11.10. Also when i put main.go file from any of the cmd directories to project root directory and build, the compiler builds it just fine (binary file size is ~33mb).

Is it possible to use 2 compiler entry points in a single project?

ik9999
  • 33
  • 1
  • 5
  • Your project seems to be inside your gopath, so you don't need to turn modules on. I don't think that you have a compile error, if the program compiles. So what is inside of your main files? – apxp Jun 08 '19 at 19:20

1 Answers1

0

You can use go install ./... and it will create the executables in $GOPATH/bin directory if you are looking to get the executables

And regarding the multiple compiler entry points, you can. You can build using go build ./cmd/c ./cmd/d . but you cannot get the executables as per the GO Documentation -o can be only used in the case of single package. Instead you can write a makefile to get all the executables with a single make target.

And regarding the error that you are seeing, I would need more information. When I tried to build a sample application, everything works fine. I am not using GO111MODULE flag though.

enter image description here

  1. go build gives me stackoverflow executable
  2. go build -o test ./cmd/c gives me test executable
  3. go build ./cmd/c gives me c executable

For your convenience I uploaded the project to github repo

Nihanth Dara
  • 108
  • 1
  • 5
  • 1
    Thank you. Setting package name to `main` in main.go files inside cmd directory fixed my problem. I had "package c" and "package d" there before. – ik9999 Jun 08 '19 at 19:20