首页 > 解决方案 > Go 模块:go/types conf.Check 返回错误:无法导入 github.com/xxx/yyy/sub

问题描述

我有一个使用 Go 模块的项目,内容go.mod

module github.com/xxx/yyy

然后,我创建了一个子目录log和一个源文件log.go,项目结构如下:

yyy
- go.mod
- main.go
- log
  - log.go

然后我把代码写在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
    }
}

然后我成功构建它,但是当我运行它时,它会崩溃并记录could not import github.com/xxx/yyy/log。错误来自conf.Check肯定,但为什么它会崩溃?不go/types支持 go 模块?

标签: gocompiler-errors

解决方案


推荐阅读