【发布时间】:2021-10-04 12:40:22
【问题描述】:
使用 Rust,只需在与目标关联的 cargo.toml 文件中指定它们即可生成多个二进制目标:
[[bin]]
name = "daemon"
path = "src/daemon/bin/main.rs"
[[bin]]
name = "client"
path = "src/client/bin/main.rs"
有没有办法用 Go 模块实现相同的功能,即使用一个“go build”命令构建多个可执行文件?
问题示例
//main1.o
//package main
func main() {
println("From main1")
}
// - -
//main2.o
//package main
func main() {
println("From main2")
}
// ---
go build:
// ---
//Result:
./main2.go:3:6: main redeclared in this block
/Users/sergehulne/Documents/code/Go/tst/main1.go:3:6: previous declaration
【问题讨论】:
-
根据documentation,可以将多个包传递给
go build。你可以试试这个吗? -
据我所知,您无法使用“go build”构建多个可执行文件。构建 bash 脚本的问题的解决方案。这是一篇可能对您有帮助的文章digitalocean.com/community/tutorials/…
-
因此,除了将您的项目划分为多个模块(一个用于项目所需的每个二进制可执行文件)之外,没有其他解决方案。
-
很多模块都有多个主包,你只需要单独构建它们。