I have a project which is currently under my GOPATH
/usr/local/go/src/
Then I went inside my project root and ran:
go get -u golang.org/x/vgo
In my main.go I want to use lib httprouter so I added in import statement:
import ( "github.com/julienschmidt/httprouter" )
I ran
vgo build
- Then when I start to run my server as usual I get the following error:
main.go:8:2: cannot find package "github.com/julienschmidt/httprouter" in any of:
/usr/local/go/src/github.com/julienschmidt/httprouter (from $GOROOT)
/Users/myuser/go/src/github.com/julienschmidt/httprouter (from $GOPATH)
I understand the error, it seems to be searching for httprouter under GOPATH or GOROOT. So do I have to do anything else to just let vgo do its thing and I can run my server successfully?
Using
vgo build
Here is a "Hello, World" walk-through of using
vgo
, including common errors and how to respond to them.$ go get -u golang.org/x/vgo
$ cd /tmp/hello # Outside of GOPATH.
$ cat main.go
$ vgo build
This error tells us we need to specify what the root of our vgo module is. A module is a collection of packages (possibly one) versioned together. Create an empty
go.mod
file to signify this is the module root:Try building again:
$ vgo build
.This error tells us vgo doesn't know the import path of our package. There are two ways to tell it:
module
statement togo.mod
.import
comment to our package.We'll go with option 2 right now and come back to option 1. Change the first line of
main.go
to be:$ vgo build
one last time.Run the binary:
$ ./hello
Note:
go.mod
now contains amodule
line (option 1 above):If you are inside your
GOPATH
, the error in step 5 should not occur -vgo build
should automatically figure out you're inside yourGOPATH
and add themodule
line accordingly.Using
vgo run
(without usingvgo build
)For this example, lets use the
hello.go
from the A Tour of Versioned Go (vgo):$ go get -u golang.org/x/vgo
$ mkdir /tmp/hello2
$ curl -sS https://swtch.com/hello.go >hello.go
$ vgo run hello.go
$ touch go.mod
$ vgo run hello.go