Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 659
Jimmy Gong Avatar asked Apr 16 '16 15:04

Jimmy Gong


People also ask

How do I compile a .go file?

From the command line in the hello directory, run the go build command to compile the code into an executable. From the command line in the hello directory, run the new hello executable to confirm that the code works.

Does go compile to binary?

To complete the list, go run compiles your application into a temporary folder, and starts that executable binary. When the app exits, it properly cleans up the temporary files.

Where does go build put binaries?

This command does perform the exact operation as go build but places the binary in $GOPATH/bin` directory alongside the binaries of third-party tools installed via go get now if you run $GOPATH/bin/hello you will see Hello, world!

Where does go install put things?

go install in Go 1.16 Much like the previous behaviour of go get , go install places binaries in $GOPATH/bin , or in $GOBIN if set.

How does gitignore work in Git?

Here's how it works. A .gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .gitignore files.

How do I see the path of a gitignore file?

git check-ignore -v www/yarn.lock The output shows the path to the gitignore file, the number of the matching line, and the actual pattern. www/.gitignore:31:/yarn.lock www/yarn.lock The command also accepts more than one filename as arguments, and the file doesn’t have to exist in your working tree.

Can I have multiple gitignore files in the root directory?

However, you can choose to define multiple .gitignore files in different directories in your repository. Each pattern in a particular .gitignore file is tested relative to the directory containing that file. However the convention, and simplest approach, is to define a single .gitignore file in the root.

How to create binary file instead of using go build *go?

cd to the directory where you have the main.go file. Where DirectoryPath is the location where you want to create the binary file to. Show activity on this post. Instead of using go build *.go, you can do following: Define GOBIN and GOPATH environment variable.


2 Answers

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.

like image 79
Santosh Pillai Avatar answered Sep 27 '22 19:09

Santosh Pillai


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.

like image 30
Parth Desai Avatar answered Sep 27 '22 19:09

Parth Desai