dongma6326
2019-06-08 18:25
浏览 169
已采纳

使用/ cmd结构构建Go项目时出错(多个入口点)

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?

图片转代码服务由CSDN问答提供 功能建议

这是我的项目的目录结构(〜/ go / src / bitbucket.org / a / b ):

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

编译2个二进制文件(cmd /中每个模块一个)。

我尝试从项目根目录运行 GO111MODULE = on build ./cmd/c (< 代码>〜/去/ SRC / bitbucket.org / A / b )。

我也尝试过运行 GO111MODULE = on build -o test ./cmd/c 。 它创建了29kb的 test 文件。 当我向其添加执行权限并运行时,它以以下错误结束:

  ./ test:2:./ test:语法错误:换行符意外
    
 
 

我尝试使用go 1.12.5和go 1.11.10。 此外,当我将任何cmd目录中的 main.go 文件放入项目根目录时 目录并进行构建,编译器会很好地进行构建(二进制文件大小约为33mb)。

是否可以在单个项目中使用2个编译器入口点?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpcyx08288 2019-06-08 19:12
    已采纳

    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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题