Search code examples
govgo

Go modules are not found using VGO and go.mod


I'm using versioned Go command to manage my vendors, everything is working but when I run go build it doesn't find my local packages

I have set the module root inside my go.mod with I still get an error

build foo: cannot find module for path

The project arch is like

foo/
|__src/github.com/username/package1/package1.go
|__src/github.com/username/package2/package2.go
|__src/github.com/username/package3/package3.go
|__main.go
|__go.mod
|__go.sum

So my go.mod look like

module foo

require (
    ...
)

I followed https://research.swtch.com/vgo-tour but I don't understand why this is not working.

My Go version is 1.11 and the foo folder is inside my GOPATH when I try outside the GOPATH this is not even working.

The only time I made it work is doing

module github.com/username/package1

require (
    ...
)

but the 2 other packages are not found and I get the same error as above.

Did I miss something or do the module path I provide must be changed ?


Solution

  • I assume your local packages imported are wrong, follow my example.

    There is my go.mod (outside of GOPATH, I've imported mux for example):

    module example
    
    require github.com/gorilla/mux v1.6.2 // indirect
    

    BTW you can create an empty go.mod, go will find and update your go.mod for you.

    The main.go:

    package main
    
    import (
        _ "example/src/foo" // local package
        "fmt"
        _ "github.com/gorilla/mux" // example import
    )
    
    func main() {
        fmt.Println("foo")
    }
    

    The foo local package:

    package foo
    
    import "fmt"
    
    func bar() {
        fmt.Println("foo")
    }
    

    The module tree:

    ├── go.mod
    ├── go.sum
    ├── main.go
    └── src
        └── foo
            └── foo.go
    

    You can find more explanation here Modules