Divlaker
Divlaker

Reputation: 419

How to build a .a file in golang

I tried go build xxx.go, no file output and no error prompt. I searched and found a solution said should use go install xxx.go, got a error :

go install: no install location for .go files listed on command line (GOBIN not set)

I search this error and found the solution is set the environment variable GOBIN, GOPATH and I did but it still does not work.

Upvotes: 2

Views: 3100

Answers (2)

Divlaker
Divlaker

Reputation: 419

I solved myself. The key is: after type go build xxx.go, nothing generate, but it's not an error!

I must continue type go install sourcedir, the source must inside a directory, then I found the .a file in pkg folder.

Upvotes: 1

jeevatkm
jeevatkm

Reputation: 4791

go install: no install location for .go files listed on command line (GOBIN not set)

Create a bin directory under GOPATH parallel to src.

For e.g:

GOPATH=/home/user/go
GOBIN=$GOPATH/bin

EDIT: after comment interaction.

Create your go workspace, refer to workspace doc

For e.g.:

Directory Structure: GOPATH is /home/user/go

/home/user/go/src/pic-project
  └── pic.go

Go to /home/user/go/src/pic-project

go build pic.go

ls -ltr
-rw-r--r--  1 jeeva  staff       84 Jun 23 23:55 pic.go
-rwxr-xr-x  1 jeeva  staff  1624096 Jun 24 00:02 pic

Binary is in the same directory.

Now, let's do go install, you can execute install command in following ways.

Inside project directory (binary will be in $GOPATH/bin directory)

go install

OR from anywhere in the terminal-

go install pic-project

Also if you have project with main func and sub packages. Execute go install <import-path>, it will produce binary and sub-packages as .a files.

go install github.com/user/foo

You will find foo.a under $GOPATH/pkg/GOOS_GOARCH/github.com/user/foo.a and binary in the $GOPATH/bin directory.

Upvotes: 0

Related Questions