Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build binary in package in Golang?

Tags:

windows

go

build

I have a project in github.com/user called project:

src/
    github.com/user/project
        sub1/
            main.go
            sub1.exe (??)
        sub2/
            main.go
            sub2.exe (??)

I am trying to compile the package in my project. When I:

$ cd github.com/user/project/sub1
$ go build

Nothing happens. go build seems to finish without complaining, but there is no executable file. How can I build packages into executable?

"go version go1.3 windows/amd64"

like image 899
Kiril Avatar asked Feb 08 '15 15:02

Kiril


People also ask

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!

Does Go compile to binary?

While the go run command is a useful shortcut for compiling and running a program when you're making frequent changes, it doesn't generate a binary executable.

What are binaries in go?

Go Binaries is an open-source server allowing non-Go users to quickly install tools written in Golang, without installing the Go compiler or a package manager — all you need is curl .

How do I create an executable in golang?

To build an executable, the command would take this form: GOOS= target-OS GOARCH= target-architecture go build package-import-path.


1 Answers

It doesn't matter if you name your file main.go or shubudoo.go. The only thing what matters if you want to build an executable (a command) is that your files start with package main. One more thing: Go has absolutely no notion of "subpackage": All packages are equal for the compiler. The filesystem nesting is for your convenience only.

like image 151
Volker Avatar answered Nov 15 '22 05:11

Volker