简体   繁体   English

使用VGO和go.mod找不到Go模块

[英]Go modules are not found using VGO and go.mod

I'm using versioned Go command to manage my vendors, everything is working but when I run go build it doesn't find my local packages 我正在使用版本化的Go命令来管理我的供应商,一切正常,但是当我运行go build时,找不到本地软件包

I have set the module root inside my go.mod with I still get an error 我在go.mod设置了模块根目录,但仍然出现错误

build foo: cannot find module for path build foo:找不到路径模块

The project arch is like 项目拱门就像

foo/
|__src/github.com/username/package1/package1.go
|__src/github.com/username/package2/package2.go
|__src/github.com/username/package3/package3.go
|__main.go
|__go.mod
|__go.sum

So my go.mod look like 所以我的go.mod看起来像

module foo

require (
    ...
)

I followed https://research.swtch.com/vgo-tour but I don't understand why this is not working. 我关注了https://research.swtch.com/vgo-tour,但是我不明白为什么这不起作用。

My Go version is 1.11 and the foo folder is inside my GOPATH when I try outside the GOPATH this is not even working. 我去的版本是1.11foo文件夹是我的内GOPATH当我尝试外GOPATH这甚至没有工作。

The only time I made it work is doing 我让它工作的唯一时间是

module github.com/username/package1

require (
    ...
)

but the 2 other packages are not found and I get the same error as above. 但找不到其他2个软件包,并且出现与上述相同的错误。

Did I miss something or do the module path I provide must be changed ? 我错过了什么吗?必须更改提供的模块路径吗?

I assume your local packages imported are wrong, follow my example. 我假设您导入的本地软件包错误,请按照我的示例进行操作。

There is my go.mod (outside of GOPATH , I've imported mux for example): 有我的go.mod (在go.mod之外, GOPATH ,我已导入mux ):

module example

require github.com/gorilla/mux v1.6.2 // indirect

BTW you can create an empty go.mod , go will find and update your go.mod for you. 顺便说一句,您可以创建一个空的go.modgo会为您查找并更新go.mod

The main.go : main.go

package main

import (
    _ "example/src/foo" // local package
    "fmt"
    _ "github.com/gorilla/mux" // example import
)

func main() {
    fmt.Println("foo")
}

The foo local package: foo本地软件包:

package foo

import "fmt"

func bar() {
    fmt.Println("foo")
}

The module tree: 模块树:

├── go.mod
├── go.sum
├── main.go
└── src
    └── foo
        └── foo.go

You can find more explanation here Modules 你可以在这里找到更多的解释Modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM