Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make go import packages from vendor?

I was checking the vendor feature in go with glide. It was awesome installing all packages but I couldn't manage to make go command to found them in the vendor packages.

go run src/main.go

src/main.go:8:2: cannot find package "github.com/valyala/fasthttp" in any of:
   /home/joaonrb/.software/lib/go/go1.7/src/github.com/valyala/fasthttp (from $GOROOT)
   /home/joaonrb/.projects/go-blog/src/github.com/valyala/fasthttp (from $GOPATH)

Fasthttp is installed in /home/joaonrb/.projects/go-blog/src/vendor/github.com/valyala/fasthttp, the version of go I'm using is 1.7 and my GOPATH is /home/joaonrb/.projects/go-blog

like image 340
joaonrb Avatar asked Sep 16 '16 14:09

joaonrb


People also ask

How do I import a package into Go?

To import a package, we use import syntax followed by the package name. 💡 Unlike other programming languages, a package name can also be a subpath like some-dir/greet and Go will automatically resolve the path to the greet package for us as you will see in the nested package topic ahead.

How does Go mod vendor work?

The go mod vendor command constructs a directory named vendor in the main module's root directory that contains copies of all packages needed to support builds and tests of packages in the main module. Packages that are only imported by tests of packages outside the main module are not included.

How do I import multiple packages in Go?

2. Grouped import. Go also supports grouped imports. This means that you don't have to write the import keyword multiple times; instead, you can use the keyword import followed by round braces, (), and mention all the packages inside the round braces that you wish to import.

Where does Go mod store packages?

What is Go Module. A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root.


1 Answers

Your GOPATH structure does not seem valid. To do what you want:

  • Create a "project" folder, e.g. $GOPATH/src/myproj.
  • Put your main.go there.
  • Create a vendor folder there, e.g. $GOPATH/src/myproj/vendor.
  • Put github.com/valyala/fasthttp there.

That should work.

like image 90
Ainar-G Avatar answered Oct 01 '22 05:10

Ainar-G