英文:
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标志。
-
进入包含
main.go文件的目录。 -
使用以下命令 -
go build -o 目录路径
其中,目录路径是你想要创建二进制文件的位置。
英文:
you can use the -o flag.
-
cdto the directory where you have themain.gofile. -
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,可以按照以下步骤操作:
- 设置
Go工作区,使用官方的Go资源:如何编写Go代码 - 定义
GOBIN和GOPATH环境变量。GOBIN环境变量将指向您想要存储可执行文件的目录。GOPATH环境变量将指向您设置Go工作区的目录。
现在,不再使用go build *.go,而是使用go install {YourCodePath},其中{YourCodePath}是Go工作区中代码的相对路径。如果构建成功,您可以在GOBIN指定的目录中找到可执行文件。
英文:
Instead of using go build *.go, you can do following:
- Setup
Go workspace, using this official go resource: How to write go code - Define
GOBINandGOPATHenvironment variable.GOBINenvironment variable will point to the directory where you want to store your executables.GOPATHenvironment variable will point to the directory, where you've setupgo 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论