I have a project using Go module, and the content of go.mod
is
module github.com/xxx/yyy
Then, I create a sub directory log
and a source file log.go
, the project structure looks like this:
yyy
- go.mod
- main.go
- log
- log.go
Then I write the code in main.go
import (
"github.com/xxx/yyy/log"
"go/ast"
"go/build"
"go/importer"
"go/parser"
"go/token"
"go/types"
)
var fileSet = token.NewFileSet()
func main() {
file, err := parser.ParseFile(fileSet, "main", nil, parser.ParseComments)
conf := types.Config{Importer: importer.Default()}
pkg, err := conf.Check(".", fileSet, []*ast.File{file}, nil)
if err != nil {
log.Log.Fatal(err.Error()) // type error
}
}
Then I build it successfully, but when I run it, it crashes and logs could not import github.com/xxx/yyy/log
. The err comes from conf.Check
definitely, but why does it crash? Does go/types
not support go module?