go语言项目中在main.go中引用自己的包报错,提示cannot find module providing package

在自己go项目中,在main.go文件中引用自定义模块,一直提示cannot find module providing package。编译不过去是什么原因呢?路径都没有问题。
整个项目结构如图:

clipboard.png

clipboard.png
main.go中报错,提示build limservice: cannot load limsystem/controller: cannot find module providing package limsystem/controller
编译的时候也提示这个错误,编译不了,这个是怎么回事呢?
一下是我的系统环境设置

clipboard.png

阅读 45.1k
11 个回答
新手上路,请多包涵

在 go module 的模式下,需要指明 main 包的 “mod 名称”(我也不确定是不是这么叫的),你可以试试 go build github.com/liemei/limservice

go env环境

GO111MODULE="on"
GOARCH="amd64"
GOCACHE="/Users/Apple/Library/Caches/go-build"
GOENV="/Users/Apple/Library/Application Support/go/env"
GOPATH="/Users/Apple/Documents/Development/GolangDev"

项目目录

➜  Go tree
.
├── go.mod
├── go.sum
└── helloworld
    └── main.go

1 directory, 3 files

helloworld是你的项目目录

使用go mod init helloworld创建go.mod文件

#go.mod内容
module  helloworld
go  1.14
require  github.com/imroc/req  v0.3.0

#helloworld中main.go内容
package main
import (
    "fmt"
    "github.com/imroc/req"
)
func  main() {
    r, _  := req.Get("http://www.baidu.com")
    fmt.Println(r)
}

改为github.com/limsystem/controllersrc下的要全路径

还有就是目录应该是这样。

|-- golang-projects 
    |-- bin
    |-- pkg
    |-- src
       |-- github.com
          |-- [your-github-name]
              |-- [your-project-name]         

因为你本地配置不正确,首先会去github.com/[your-github-name]/[your-project-name] 获取库的。
或者把你代码移到src同级

clipboard.png
换成完整的github.com/limsystem/controller后一样报错....

——————————————————————————————————————
我修改了目录,在外边增加了一层也不行

clipboard.png
我的go.mod内容如下:

//module limservice

module github.com/liemei/limservice

go 1.12

require (
    github.com/BurntSushi/toml v0.3.1 // indirect
    github.com/Joker/jade v1.0.0 // indirect
    github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 // indirect
    github.com/ajg/form v1.5.1 // indirect
    github.com/aymerick/raymond v2.0.2+incompatible // indirect
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
    github.com/fatih/structs v1.1.0 // indirect
    github.com/flosch/pongo2 v0.0.0-20190505152737-8914e1cf9164 // indirect
    github.com/gavv/monotime v0.0.0-20190418164738-30dba4353424 // indirect
    github.com/golang/protobuf v1.3.1 // indirect
    github.com/google/go-querystring v1.0.0 // indirect
    github.com/imkira/go-interpol v1.1.0 // indirect
    github.com/iris-contrib/blackfriday v2.0.0+incompatible // indirect
    github.com/iris-contrib/formBinder v0.0.0-20190104093907-fbd5963f41e1 // indirect
    github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
    github.com/iris-contrib/httpexpect v0.0.0-20180314041918-ebe99fcebbce // indirect
    github.com/json-iterator/go v1.1.6 // indirect
    github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
    github.com/kataras/golog v0.0.0-20180321173939-03be10146386 // indirect
    github.com/kataras/iris v11.1.1+incompatible
    github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d // indirect
    github.com/klauspost/compress v1.6.2 // indirect
    github.com/klauspost/cpuid v1.2.1 // indirect
    github.com/mattn/go-colorable v0.1.2 // indirect
    github.com/microcosm-cc/bluemonday v1.0.2 // indirect
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.1 // indirect
    github.com/moul/http2curl v1.0.0 // indirect
    github.com/onsi/ginkgo v1.8.0 // indirect
    github.com/onsi/gomega v1.5.0 // indirect
    github.com/ryanuber/columnize v2.1.0+incompatible // indirect
    github.com/sergi/go-diff v1.0.0 // indirect
    github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
    github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
    github.com/stretchr/testify v1.3.0 // indirect
    github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
    github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
    github.com/xeipuuv/gojsonschema v1.1.0 // indirect
    github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
    github.com/yudai/gojsondiff v1.0.0 // indirect
    github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
    github.com/yudai/pp v2.0.1+incompatible // indirect
    golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect
    golang.org/x/net v0.0.0-20190603091049-60506f45cf65 // indirect
    golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed // indirect
    golang.org/x/text v0.3.2 // indirect
    gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

把你的env环境配置贴出来,你的项目main.go应该单独一个目录,不应该放下github.com这个目录里面,应该放在src目录和github.com同级。引用目录github.com/limsystem/controller。

你的问题解决了嘛,我也遇到了类似的问题

go.mod 文件里module 是limsystem 吗?

GoLand的配置里开启go mod了吗

clipboard.png

新手上路,请多包涵

把所有的go mod的文件删了重新生成一遍就好了

gomod配置出现问题 goland内部需要配置go Moduled mode里package的包名和你的main的相同

我当时碰到问题是因为我一开始设置了环境变量,所以导致报这个错。

go env -w GO111MODULE=on //当时执行过这条命令

所以可以使用命令,检查下Go配置,是否GO111MODULE=on

go env //检查配置

解决方法:在自己的项目目录下,比如我项目叫crm,使用以下命令

go mod init crm
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题