Golang Binary Only Package: Full Example

Golang binary only package tutorial

Golang binary only packageYou may need to distribute a Go package binary rather than the source code files. This example walks you through building and distributing a binary package.

Be aware that the go get functionality does not work with binary-only packages.


Build original package

vi $GOPATH/src/mypkg/mypkg.go

Build binary package

cd $GOPATH/src/mypkg/
cat mypkg.go
go build -o $GOPATH/pkg/darwin_amd64/mypkg.a -x

Move existing source

Move original sources file since you will replace it with the special file in the next step
mkdir $GOPATH/src/mypkg-temp
mv $GOPATH/src/mypkg/mypkg.go $GOPATH/src/mypkg-temp/

Create special source

Add indicator toward the top of the new source file and package statement at a minimum. You may choose to add package documentation below this.
touch $GOPATH/src/mypkg/mypkg.go
vi $GOPATH/src/mypkg/mypkg.go


Zip for distribution

The zip file will include the pkg directory that contains the package binary and src directory that contains a source file with the “//go:binary-only-package” indicator.
zip -r binarypkg.zip src/mypkg/mypkg.go pkg/darwin_amd64/mypkg.a

Unzip on target

The target computer should run the same Go version as the Go version used to compile the binary.
unzip binarypkg.zip -d $GOPATH/

Test binary package

touch test.go
vi test.go

go run test.go

See also:
Golang SSH Client
Golang DNS Lookup
Golang IP Address Manipulation
Golang Regular Expression Match