英文:
Is there a canonical way to add build scripts to a Go project?
问题
来自Node.js世界的经验是,每当你需要一个脚本(比如构建脚本),处理的常见方式是将其添加到package.json
文件的scripts
块中,这样你可以通过调用npm run <scriptname>
来运行脚本。换句话说,Node.js(以及npm)有一种内置的方式来处理(简单的)脚本。
在Go世界中是否存在类似的机制呢?
换句话说,假设我有一些命令,比如构建、清理构建目录、创建新版本等等,有没有比添加一堆(依赖于平台的)Bash脚本文件更好的方法来处理这些命令呢?
如果没有的话,是否至少有一种常见的方式来确定这些脚本的放置位置?或者这完全由开发者自行决定?
英文:
Coming from the Node.js world, whenever you need a script (such as a build script there), a very common way of handling this is by adding it to the scripts
block in the package.json
file, so that you can the script by calling npm run <scriptname>
. In other words, Node.js (respectively npm) has a built-in way to deal with (simple) scripts.
Does an equivalent to this exist in the Go world?
To put it differently: Suppose I have some commands such as doing a build, cleaning the build directory, creating a new version, and so on, is there a better way to do this than by adding a bunch of (platform-dependant) Bash script files?
In case there isn't – is there at least a common way of where to put these scripts? Or is this all left to the developer?
答案1
得分: 3
你可以使用Makefile
。
这是我有时候使用的一个示例:
BINARY_NAME=main
build:
go mod tidy
go build -o ${BINARY_NAME} main.go
clean:
go clean
rm ${BINARY_NAME}
英文:
You can use Makefile
this one is what I use sometimes:
BINARY_NAME=main
build:
go mod tidy
go build -o ${BINARY_NAME} main.go
clean:
go clean
rm ${BINARY_NAME}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论