首页 > 解决方案 > 无法从 GO 中的其他文件导入包

问题描述

我正在使用go1.13.4,下面是我的项目结构:

src/types/type.go
src/utils/util.go
go.mod

在 go.mod 中:

module example.com/graphql

go 1.13

require github.com/graph-gophers/graphql-go v0.0.0-20191031232829-adde0d0f76a3

在 src/types/type.go 中:

package types

type USER struct {
   ...
}

在 src/utils/util.go 中,

package utils
import (
    "example.com/graphql/types"
    "fmt"
    "io/ioutil"
    "os"
)

构建项目时出现棕褐色错误:

$ go build ./...
src/utils/utils.go:4:2: cannot find module providing package example.com/graphql/types

我想知道为什么它找不到包裹example.com/graphql/types?我正在阅读https://blog.golang.org/using-go-modulesgo.mod并且我已经在项目根目录的文件中设置了模块名称。

标签: gogo-modules

解决方案


使用您当前的布局,导入路径typesexample.com/graphql/src/types.

go.modsrc如果你有那个结构,应该在里面。或者更好的是摆脱src. go.mod必须在typesandutils文件夹旁边。


推荐阅读