Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 222
Freewind Avatar asked May 27 '12 09:05

Freewind


People also ask

How do I install third party packages in Golang?

We can download and install third-party Go packages by using “Go get” command. The Go get command will fetch the packages from the source repository and put the packages on the GOPATH location. The MongoDB driver, mgo, provides two packages that we have imported in the above import statement.

How do I install packages to Go?

To install a package using go get follow the following steps: Step 1: Make sure to check whether the Golang is installed on your system by checking the version of Go. Step 2: Set the GOPATH by using the following command. Step 3: Now, set the PATH variable with the help of the following command.

Where does Go install packages Go?

Install compiles and installs the packages named by the import paths. Executables are installed in the directory named by the GOBIN environment variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH environment variable is not set.


2 Answers

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.

like image 171
Freewind Avatar answered Sep 21 '22 07:09

Freewind


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

like image 35
rofrol Avatar answered Sep 21 '22 07:09

rofrol