如何从下载源安装Golang第三方项目?

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

How to install golang 3rd-party projects from download sources?

问题

我正在尝试安装mgo,它是一个用golang编写的mongo驱动程序。

标准命令是:

go get launchpad.net/mgo

但由于一些证书问题,它失败了。

所以我手动将mgo的源代码下载到本地的E:\mgo目录下,但我不知道如何安装它。

文件树结构如下:

├─.bzr
│  ├─branch
│  │  └─lock
│  ├─branch-lock
│  ├─checkout
│  │  └─lock
│  └─repository
│      ├─indices
│      ├─lock
│      ├─obsolete_packs
│      ├─packs
│      └─upload
├─bson
└─testdb

我尝试了:

cd mgo
go install

它报告了以下错误:

auth.go:34:2: import "launchpad.net/mgo/bson": cannot find package

但如果我先尝试安装bson:

cd bson
go install

它报告了另一个错误:

go install: no install location for _/E_/mgo/bson

那么,安装它的正确命令是什么?

英文:

I'm trying to install mgo which is a mongo-driver written in golang.

The standard command:

go get launchpad.net/mgo

But it failed because of some cert issues.

So I manually download the sources of mgo to local E:\mgo, but I don't know to how install it.

The file tree:

├─.bzr
│  ├─branch
│  │  └─lock
│  ├─branch-lock
│  ├─checkout
│  │  └─lock
│  └─repository
│      ├─indices
│      ├─lock
│      ├─obsolete_packs
│      ├─packs
│      └─upload
├─bson
└─testdb

I tried:

cd mgo
go install

It reports:

auth.go:34:2: import "launchpad.net/mgo/bson": cannot find package

But if I try to install bson first:

cd bson
go install

It reports another error:

go install: no install location for _/E_/mgo/bson

So, what's the correct command to install it?

答案1

得分: 46

最后我成功安装了mgo项目。我认为这对初学者很有帮助,所以我在这里回答它。

首先,我们需要GOPATH。

定义一个名为GOPATH的环境变量,它是你的项目根目录,应该有一个名为src的子目录。

对于我来说,我将其定义为E:\WORKSPACE_GO\mgo,然后创建一个名为src的子目录。

将项目复制到src目录中。

然后将mgo项目复制到%GOPATH%/mgo,但是我们必须小心目录结构。它应该与源代码中定义的包完全相同。

对于mgo,它的包是launchpad.net/mgo,所以目录结构应该是:

E:\WORKSPACE_GO\mgo\src\launchpad.net\mgo

最后,使用go install安装它们:

cd E:\WORKSPACE_GO\mgo\src\launchpad.net\mgo\bson
go install

cd ..
go install

如果没有错误输入,它应该成功安装。

英文:

Finally I successfully install the mgo project. I think it will be helpful for beginners, so I answer it here.

First, we need GOPATH

Define a env variable GOPATH, which is your project root directory, and it should have a sub dir src.

For me, I define it to E:\WORKSPACE_GO\mgo, then create a sub dir src

Copy the project to the src

Then copy the mgo project to %GOPATH%/mgo, but we must be careful about the directory structure. It should be exactly the same as the package defined in sources.

For mgo, it's package is launchpad.net/mgo, so the structure should be:

E:\WORKSPACE_GO\mgo\src\launchpad.net\mgo

go install

At last, go install them:

cd E:\WORKSPACE_GO\mgo\src\launchpad.net\mgo\bson
go install

cd ..
go install

If there is no error input, it should be successfully installed.

答案2

得分: 29

设置 GOPATH。将代码移动到$GOPATH下。然后执行以下命令:

cd $GOPATH/src/github.com/user/package
go get .

解释:

go build .    # 在当前目录生成二进制文件
go install .  # 在$GOPATH/bin目录生成二进制文件
go get .      # 与'install'相同,但会解析导入的依赖项

更多信息

英文:

Set GOPATH. Move code under $GOPATH. Then

cd $GOPATH/src/github.com/user/package
go get .

Explanation:

go build .    # produces binary in current dir 
go install .  # produces binary in $GOPATH/bin 
go get .      # same as 'install' but resolves import deps 

More on that

答案3

得分: 5

你需要将它放入你的GOPATH/src目录中,保留导入路径(你传递给go install的路径)。

回退的GOPATH是你的go install目录。所以你可以将mgo存储库克隆到go/src/pkg/launchpad.net/mgo

或者,当你为依赖于mgo的项目设置一个项目目录/环境,并将GOPATH环境变量设置为该文件夹时,你可以将mgo存储库克隆到FOLDER/src/launchpad.net/mgo/

然后你可以像预期的那样在你的项目中使用mgo包:

import "launchpad.net/mgo"
英文:

You will have to put it into your GOPATH/src directory, preserving the import path (the one you passed to go install).

The fallback GOPATH is your go install directory. So you can clone the mgo repository to go/src/pkg/launchpad.net/mgo.

Alternatively, when you set up a project directory/environment for your project that depends on mgo, and set the GOPATH environment variable to that FOLDER, then you can clone the mgo repository to FOLDER/src/launchpad.net/mgo/.

You can then use the mgo package in your project as expected:

import "launchpad.net/mgo"

答案4

得分: 0

这一切的瑕疵是,GOPATH是一个路径 - 类似于Java的类路径或Unix的路径。它不是一个单一的目录位置:它是一系列目录位置。例如,我经常使用以下形式的语句:

GOPATH=dira:dirb:dirc go install mypackage
英文:

The fly in the ointment of all this, is that GOPATH is a path - similar to Java's classpath, or Unix's PATH. It is not a single directory location: it is a sequence of directory locations. E.g., I routinely uses statements of the form,

GOPATH=dira:dirb:dirc go install mypackage

huangapple
  • 本文由 发表于 2012年5月27日 17:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/10772799.html
匿名

发表评论

匿名网友

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

确定