lundi 29 avril 2019

go test fails on import of package in module under test

What is the correct way to import a package in the same module? When I use a fully qualified module path, the build works correctly but testing fails due to the import:

$ tree
.
├── app
│   └── main.go
├── go.mod
└── lib
    └── something.go

2 directories, 3 files

$ cat go.mod
module github.com/myname/mypackage

go 1.12

$ cat app/main.go 
package main

import (
    "fmt"
    "github.com/myname/mypackage/lib"
)

func main() {
    fmt.Println(lib.Var1)
}

$ cat lib/something.go 
package lib

var Var1 = 42

$ go build app/main.go 
$ ./main
42

$ go test
can't load package: package github.com/myname/mypackage: unknown import path "github.com/myname/mypackage": cannot find module providing package github.com/myname/mypackage

The module is not in the go path.

Aucun commentaire:

Enregistrer un commentaire