如何在Golang中构建.a文件

huangapple go评论75阅读模式
英文:

How to build a .a file in golang

问题

我尝试了go build xxx.go,没有生成文件也没有错误提示。我搜索到一个解决方案说应该使用go install xxx.go,但是出现了一个错误:

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

我搜索了这个错误并找到了解决方案,说要设置环境变量GOBIN和GOPATH,我已经设置了,但仍然不起作用。

英文:

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.

答案1

得分: 1

我解决了。关键是:在输入go build xxx.go后,没有生成任何内容,但这并不是一个错误!

我必须继续输入go install sourcedir,源代码必须在一个目录中,然后我在pkg文件夹中找到了.a文件。

英文:

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.

答案2

得分: 0

> go install:命令行上没有列出.go文件的安装位置(未设置GOBIN)

在GOPATH的src目录平行创建一个bin目录。

例如:

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

**编辑:**根据评论互动。

创建你的Go工作空间,请参考工作空间文档

例如:

目录结构:GOPATH为/home/user/go

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

进入/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

二进制文件位于相同的目录中。

现在,让我们执行go install,你可以通过以下方式执行安装命令。

在项目目录内(二进制文件将位于$GOPATH/bin目录中)

go install

或者在终端的任何位置执行以下命令-

go install pic-project

此外,如果你的项目有主函数和子包,执行go install <import-path>,它将生成二进制文件和子包作为.a文件。

go install github.com/user/foo

你将在$GOPATH/pkg/GOOS_GOARCH/github.com/user/foo.a下找到foo.a文件,并在$GOPATH/bin目录中找到二进制文件。

英文:

> 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 &lt;import-path&gt;, 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.

huangapple
  • 本文由 发表于 2017年6月24日 11:16:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/44732395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定