英文:
Is it possible to build a Go binary using the standard library?
问题
经过研究源代码,我认为这是不可能的,但是可以使用类似以下方式构建一个Go二进制文件:
import "package/from/stdlib"
func main() {
...
stdlib.BuildBinary(path)
...
}
链接:
- https://github.com/golang/go/blob/master/src/cmd/go/main.go
- https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go
(我正在尝试简化CI流水线中的构建阶段)
英文:
Having studied the source code I don't think it's possible, but can a Go binary be built using something like:
import "package/from/stdlib"
func main() {
...
stdlib.BuildBinary(path)
...
}
https://github.com/golang/go/blob/master/src/cmd/go/main.go
https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go
(I'm trying to simplify a build stage in a CI pipeline)
答案1
得分: 2
不,标准库不包含编译器的源代码。编译器的源代码可以在这里找到:https://github.com/golang/go/tree/release-branch.go1.20/src/cmd/compile
但是,如果你安装了标准库,那应该是Go安装的一部分,其中包含了Go工具,可以提供交叉编译支持。所以简而言之,你只需要(官方的)Go安装包。如果你想将源代码编译成可执行二进制文件,可以调用go
工具作为外部命令。
需要注意的是,尽管标准库不包含编译器和链接器的代码,但你可以使用标准库来解析Go源代码。可以参考go/ast
和go/parser
包。
英文:
No, the standard lib does not contain the compiler's (source) code. The compiler's source is available here: https://github.com/golang/go/tree/release-branch.go1.20/src/cmd/compile
But if you have the standard lib, that should be part of a Go installation, which contains the Go tool, giving you cross-compilation support. So in short: all you need is the (official) Go installation. Call the go
tool as an external command if you want to compile source code into an executable binary.
Note however that even though the standard lib does not contain the compiler and linker's code, you can use the standard lib to parse Go sources. See the go/ast
and go/parser
packages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论