Go vendoring outside $GOPATH - go

I have a project which is built in Node.js/Express.js. I want to start to rewrite this to go/iris framework. I don't want to re-factor everything into my $GOPATH and I want to keep it together my express / go / docker files for this project.
I tried to clone iris framework's git repo into a ./vendor subfolder, but using import "github.com/kataras/iris" importing nothing.
Is there a package manager which is
copying and installing packages and all of it's dependencies in my-project/vendor folder outside $GOPATH
it can update these import packages
go run/build/install outside $GOPATH
there's now any new files in $GOPATH src/pkg/bin folder when I working on a project, except this package manager
I can define dependency packages for a project like package.json file for node.js
Is there a go package manager like that?
Edit:
Running this with go command is not required.

Not possible with the current go tooling, but looks like we might get it in go 1.12 or bit later.
Proposal accepted:
cmd/go: modify the Go toolchain to work without GOPATH

Related

Is it possible to update local packages without running go install?

I am trying to import a local file into my main.go file and this tutorial (and other similar tutorials) says to run go install <path> in order to import that path as a package. This seems like a slow way to develop local packages because you would have to run go install <path> every time you want to see the changes in your local package.
Is there a faster way to import/update local packages? I am using gomon to auto-reload my code after updating it, so ideally, my code would auto-reload after updating a local package.
You should use go modules. The tutorial you mentioned appears to be older than the modules feature. In short: you can import a package, run go build, and any imported external package will automatically be downloaded for you as needed, no need to do a go get. Start here:
https://blog.golang.org/using-go-modules
https://github.com/golang/go/wiki/Modules

Cannot find package in folder vendor in vscode - Golang

I cloned code here: https://github.com/kkdai/line-login-go.
I deployed on heroku and ran successfully. But I get: cannot find package on VSCode
Normally, if I put the project in the src directory, and set GOPATH, it will not report an error.
But this project doesn't follow that structure, packages are in the vendor/ directory.
What do I have to do to handle the error?
You could try and use/add go modules to your cloned project (with Go 1.11+, and GO111MODULE=on set):
go mod init github.com/kkdai/line-login-go
go mod vendor
Then you should have all missing dependencies added to your vendor folder.
And you would not even need GOPATH anymore.
As seen in kardianos/govendor/issue 424, go mod should recognize vendor/vendor.json and import the right references previously declared and managed by govendor.

Using modules, newly installed package cannot be referenced within project

go version go1.11.4 darwin/amd64
GOPATH has been unset but was previously set to $HOME/Development/go
Project path is under $HOME/code/
I’m able to successfully (no errors at least) install the go-sql-driver/mysql package using the command
go get github.com/go-sql-driver/mysql#v1
When I include a reference to that package in an import statement
import(
_ "github.com/go-sql-driver/mysql")
in my code I see the error
could not import github.com/go-sql-driver/mysql (can’t find import:
“github.com/go-sql-driver/mysql”)
I have run go mod init in my project root and it creates a go.mod file. When I run the go get command I see a require statement added to that file for the package. But it seems the files for the package get installed in the default $HOME/go directory (since I've unset GOPATH).
Should I be doing things differently so that my import statement can find the newly installed package? Using modules shouldn't all the packages be installed in the project path somewhere?
Should I be doing things differently so that my import statement can find the newly installed package?
No. With modules there is no need to install the packages upfront at all.
Using modules shouldn't all the packages be installed in the project path somewhere?
No. They get downloaded somewhere in some format and used from that location but they are not "installed" like in the old GOPATH variant of go get.
Show output of go env and what go mod vendor produces.
I'm pretty sure I was doing things wrong. I was able to resolve this after referencing the following closely the steps documented at golang modules wiki. The summary is that there is no need to "install" a package via 'go get'. Instead simply make sure your project is initialized to use modules using the 'go mod init' command and then include the package name in an import statement. The next build event will pull down the package and all its dependencies.

Automatically import 3rd party go packages

I was wondering if there is a way to automatically download all the imports.
So let's assume I need to use github.com/gorilla/mux and several other packages in my code base. Should I manually go to my ~/go/src and run go get repo or is there a smarter way of doing dependency management. I am using Goland IDE for my development.
if there is a way to automatically download all the imports
You can download all imported pkgs and their dependencies by running go get from the command line.
I am using Goland IDE for my development
I'm using Goland too. When imports can't be found (ie the import path is highlighted in red), you can place your typing caret over it and press alt + enter and select go get ... from the popup window to automatically import.
There are several approaces:
Simply go get github.com/gorilla/mux which will download sources in your $GOPATH and will be resolved automatically when compiling
Use dependency management (godep, glide[deprecated])
Use modules (experimental feature in Go 1.11 - Module. Check more here)
If you want a good and stable solution, use dep (.First you have to install it, then run:
cd $GOPATH/src/path/to/project
dep init
dep ensure -add github.com/gorilla/mux
You will see a new folder vendor in your project and 2 new dependency configuration files Gopkg.lock and Gopkg.toml.
Read more about godep here.
Then run your main file as usual.
You can use dep package manager which will go through your code and automatically import all the packages you use in you code. If you are working with >go1.11 I would suggest to use newly added go mod.

Cannot install docker pkg dependency in Go

I'm trying to use the docker package in one of my Go applications. I'm importing the package as import "github.com/dotcloud/docker" in my script. But when trying to build the dependencies, that is, when I run go get in my project directory, it says:
foo.go:9:2: no buildable Go source files in /home/neville/gocode/src/github.com/dotcloud/docker
Here, my GOPATH is set to /home/neville/gocode, so when doing go get, the package should get downloaded to /home/neville/gocode/pkg, instead of /home/neville/gocode/src. What am I missing here?
github.com/dotcloud/docker isn't a Go package, and that's why there are no source files in that directory.
Import the package you want directly, like so for the registry package:
import "github.com/dotcloud/docker/registry"
Also, go get does download into $GOPATH/src. The installed object files go in $GOPATH/pkg.

Resources