在Go项目中,是否有一种规范的方法来添加构建脚本?

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

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 &lt;scriptname&gt;. 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}

huangapple
  • 本文由 发表于 2022年4月20日 20:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/71939622.html
匿名

发表评论

匿名网友

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

确定