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
package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}
$ vgo build
cannot determine module root; please create a go.mod file there
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:
$ touch go.mod
Try building again: $ vgo build.
vgo: cannot determine module path for source directory
/tmp/hello (outside GOPATH, no import comments)
This error tells us vgo doesn't know the import path of our package. There are two ways to tell it:
- Add a
module statement to go.mod.
- Add an
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:
package main // import "my/import/path/hello"
$ vgo build one last time.
Run the binary: $ ./hello
Hello, World
Note: go.mod now contains a module line (option 1 above):
$ cat go.mod
module my/import/path/hello
If you are inside your GOPATH, the error in step 5 should not occur - vgo build should automatically figure out you're inside your GOPATH and add the module line accordingly.
Using vgo run (without using vgo 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
cannot determine module root; please create a go.mod file there
$ touch go.mod
$ vgo run hello.go
vgo: resolving import "rsc.io/quote"
vgo: finding rsc.io/quote (latest)
vgo: adding rsc.io/quote v1.5.2
Hello, world.