How to compile .go file to specific directory, or to gitignore the binaries?

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

How to compile .go file to specific directory, or to gitignore the binaries?

问题

现在我只会运行go build __.go,但我想知道是否可以将该文件构建在一个子目录中(例如/bin文件夹)。这样可以更方便地在git中忽略二进制文件,但目前我不太确定还有什么其他好的方法,因为我也在努力创建一个有效的gitignore例外规则,而不仅仅是“忽略所有文件,除了.go文件”。

我目前的解决方案是每次构建时为二进制文件命名(例如go build -o hello.bin hello.go),但这似乎很繁琐。

英文:

Right now I'll just run go build __.go, but I'm wondering if it's possible to have that file built in a subdirectory (e.g. a /bin folder). It would just make gitignoring the binary files a lot cleaner, and right now I'm not really sure what else is a good approach as I'm also struggling to create a working gitignore exception rule that isn't just "Ignore all files, except .go files".

My current solution is naming the binary files every time I build them (e.g. go build -o hello.bin hello.go), but this seems laborious.

答案1

得分: 21

你可以使用-o标志。

  1. 进入包含main.go文件的目录。

  2. 使用以下命令 - go build -o 目录路径

其中,目录路径是你想要创建二进制文件的位置。

英文:

you can use the -o flag.

  1. cd to the directory where you have the main.go file.

  2. use this command - go build -o DirectoryPath

Where DirectoryPath is the location where you want to create the binary file to.

答案2

得分: 1

不要回答我要翻译的问题。以下是要翻译的内容:

不要使用go build *.go,可以按照以下步骤操作:

  1. 设置Go工作区,使用官方的Go资源:如何编写Go代码
  2. 定义GOBINGOPATH环境变量。GOBIN环境变量将指向您想要存储可执行文件的目录。GOPATH环境变量将指向您设置Go工作区的目录。

现在,不再使用go build *.go,而是使用go install {YourCodePath},其中{YourCodePath}是Go工作区中代码的相对路径。如果构建成功,您可以在GOBIN指定的目录中找到可执行文件。

英文:

Instead of using go build *.go, you can do following:

  1. Setup Go workspace, using this official go resource: How to write go code
  2. Define GOBIN and GOPATH environment variable.GOBIN environment variable will point to the directory where you want to store your executables.GOPATH environment variable will point to the directory, where you've setup go workspace.

Now, instead of doing go build *.go, you can use go install {YourCodePath}, where {YourCodePath}, is relative path of your code in go workspace. And if the build is successful, you can find your executable in directory, pointed by GOBIN.

huangapple
  • 本文由 发表于 2016年4月16日 23:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/36666017.html
匿名

发表评论

匿名网友

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

确定