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

4

46

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?

Heads answered 27/5, 2012 at 9:2 Comment(1)
Are your projects in your GOPATH env variable ?Bollinger
H
46

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.

Heads answered 27/5, 2012 at 9:50 Comment(4)
You can have multiple projects in your GOPATH. I usually have the standard Go repository, a go directory where I put all my external imports (from code.google.com or github.com), and all my go projects. The go tool always put the compiled things in the nearest gopath directory and always find the imports.Bollinger
Freewind, you can accept your answer. It's basically correct. The best documentation for GOPATH is golang.org/doc/code.html#tmp_13. You generally don't have to do your first step there if you have set up GOPATH previously. You leave it the same way as you work on different projects. Your copy step is correct. For go install, you don't have to install dependencies separately. You can just install the top-level commands or packages and all of the dependencies will be installed automatically.Hsiuhsu
You don’t need to go install it. As long as any project depending on that package has that GOPATH in its GOPATH (in GOPATH when building), it will find the dependency anyway. In fact, if it can find the go install-generated files, it will also find the sources and build it for itself if necessary.Fabrianna
Also one must note that Bazaar must be installed and available in PATH.Wapiti
K
29

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

Kamal answered 29/4, 2013 at 16:26 Comment(1)
I get no buildable Go source files in "said directory."Gaskins
F
5

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"
Fabrianna answered 3/6, 2012 at 17:13 Comment(0)
G
0

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
Grandparent answered 21/9, 2017 at 1:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.