go build的行为很奇怪。

huangapple go评论228阅读模式
英文:

go build behaves strangely

问题

我有以下的项目结构:

./
    lib/   共享库
    cmd/
        cli/
            main.go 
        srv/
            main.go

在main.go中,我有以下代码:

package main

import "fmt"

func main() {
	fmt.Println("cli")
}

从根目录运行go build ./...,希望在根目录下创建clisrv二进制文件,但什么都没有发生。

但是运行go build ./cmd/cli确实在根目录下产生了一个cli二进制文件。

有什么问题吗?

英文:

I have following project structure

./
    lib/   shared library
    cmd/
        cli/
            main.go 
        srv/
            main.go

in the main.go i have

package main

import "fmt"

func main() {
	fmt.Println("cli")
}

from root folder i run go build ./..., in a hope that cli and srv binaries would be created in a root folder, but nothing happens

but running go build ./cmd/cli does produce a cli binary in root folder

what is wrong?

答案1

得分: 1

Go 1.13开始,如果你将-o标志明确设置为一个目录,go build命令将输出多个可执行文件。因此,例如,mkdir -p ./bin && go build -o ./bin ./...将构建与./...匹配的所有可执行文件,并将它们输出到目录./bin中。

英文:

As of Go 1.13, go build will output multiple executables if you set the -o flag explicitly to a directory. So, for example, mkdir -p ./bin && go build -o ./bin ./... will build all executables matching ./... and output them to the directory ./bin.

huangapple
  • 本文由 发表于 2021年8月25日 18:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/68920821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定