golang: build specific version w/ -o flag

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

golang: build specific version w/ -o flag

问题

我正在尝试构建包github.com/go-delve/delve/cmd/dlv@2f13672765fe,并将生成的可执行文件命名为dlv-dap

在较旧版本的Go(1.16之前),我可以通过运行以下命令来实现:

  • go get github.com/go-delve/delve/cmd/dlv@2f13672765fe
  • go build -o dlv-dap github.com/go-delve/delve/cmd/dlv@2f13672765fe

但在v1.17中,这种方法不再适用,相反,命令go get github.com/go-delve/delve/cmd/dlv@2f13672765fe会抛出以下错误:

go: go.mod文件在当前目录或任何父目录中找不到。
在模块之外,不再支持'go get'。
要构建和安装命令,请使用带有版本的'go install',
如'go install example.com/cmd@latest'
有关更多信息,请参见https://golang.org/doc/go-get-install-deprecation
或运行'go help get'或'go help install'。

根据错误中提供的信息链接,似乎不再有一种方法可以下载/构建具有自定义名称的Golang包(例如-o my_custom_named_executable)。

我的理解是否正确,或者是否有其他方法可以实现这一点?

英文:

I am trying to build the package github.com/go-delve/delve/cmd/dlv@2f13672765fe and have the resulting executable named dlv-dap.

Under older versions of go (pre 1.16) I was able to accomplish this by running the following commands.

  • go get github.com/go-delve/delve/cmd/dlv@2f13672765fe
  • go build -o dlv-dap github.com/go-delve/delve/cmd/dlv@2f13672765fe

Under v1.17 this no longer works, instead the command go get github.com/go-delve/delve/cmd/dlv@2f13672765fe throws the following error

go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd@latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.

Reading the information link provided in the error it seems there is no longer a way to download/build a golang package with a customized name (e.g. -o my_custom_named_executable).

Is my understanding correct or is there another way to accomplish this?

答案1

得分: 6

以下是你要翻译的内容:

这里有两种解决方案可以实现你想要的功能:

第一种解决方案:使用 go install

这将会把 dlv 可执行文件安装到你的 $GOPATH/bin 目录下。

go install github.com/go-delve/delve/cmd/dlv@2f13672765fe

第二种解决方案:从源代码安装

git clone git@github.com:go-delve/delve.git
cd delve
git checkout 2f13672765fe
go build -o dlv ./cmd/dlv

这将会在项目的根目录下构建 dlv 可执行文件。

运行 Delve DAP

使用 dlv dap 子命令,dlv-dap 只是它的别名。

英文:

Here are 2 solutions to do what you want:

First solution: go install

This will install the dlv executable to your $GOPATH/bin directory.

go install github.com/go-delve/delve/cmd/dlv@2f13672765fe

Second solution: install from source:

git clone git@github.com:go-delve/delve.git
cd delve
git checkout 2f13672765fe
go build -o dlv ./cmd/dlv

This will build dlv executable to the root of the project.

Run Delve DAP

Use dlv dap subcommand, dlv-dap is just an alias to that.

答案2

得分: 0

给二进制文件命名是在 mod 文件中定义的。在这种情况下,mod 文件包含:

module github.com/go-delve/delve

如果你使用 git 克隆仓库,你可以将其更改为:

module github.com/go-delve/dev-dap

如果你现在运行 "go build",新的名称将被分配。

英文:

The name given to the binary is defined in the mod-file. In this case the mod-file contains:

module github.com/go-delve/delve

If you clone the repository using git you can change this to:

module github.com/go-delve/dev-dap

If you now start <pre>go build</pre> the new name will be assigned.

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

发表评论

匿名网友

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

确定