简体   繁体   English

使用具有典型目录结构的 go 模块,似乎无法正常工作

[英]Using go modules with a typical directory structure, can't seem to get it working

So I created a simple test project to demonstrate the issue I'm having in a much larger code base here https://github.com/cleblanc189/test.go所以我创建了一个简单的测试项目来演示我在更大的代码库中遇到的问题https://github.com/cleblanc189/test.go

.
├── go.mod
├── main.exe
├── main.go
├── server
│   ├── v1beta1
│   │   ├── foo.go
│   │   └── go.mod
│   └── v1ga
│       ├── foo.go
│       └── go.mod
└── service
    ├── v1beta1
    │   ├── foo.go
    │   └── go.mod
    └── v1ga
        ├── foo.go
        └── go.mod

I'd like to be able to reference service/v1beta1 from server/v1beta1 but I'm getting this error我希望能够从 server/v1beta1 引用 service/v1beta1 但我收到此错误

go: test.go/src/server/v1beta1@v0.0.0 requires
        test.go/src/service/v1beta1@v0.0.0: unrecognized import path "test.go/src/service/v1beta1" (https fetch: Get https://test.go/src/ser
vice/v1beta1?go-get=1: dial tcp: lookup test.go: no such host)

In foo.go from server/v1beta1 I import like this svc "test.go/src/service/v1beta1"在 foo.go 从 server/v1beta1 我像这样导入svc "test.go/src/service/v1beta1"

and have a go.mod file like below;并有一个 go.mod 文件,如下所示;

require test.go/src/service/v1beta1 v0.0.0

replace test.go/src/service/v1beta1 => ../../service/v1beta1

When I try to cd src && go mod tidy I get that error above.当我尝试cd src && go mod tidy时,我得到了上面的错误。 Also if anyone knows should I even have a go.mod above the src directory?另外,如果有人知道我什至应该在 src 目录上方有一个 go.mod 吗?

Appreciate any help anyone can offer.感谢任何人可以提供的任何帮助。 This is maddening.这令人抓狂。

Your go.mod has to declare the module path/name, typically like this:您的go.mod必须声明模块路径/名称,通常如下所示:

module test.go

Then all the import s in your code will be taken relative to that.然后,您的代码中的所有import都将与此相关。 When you import something relative to your declared module name, the go tool will automatically pick up the code from the right place locally.当您导入与声明的模块名称相关的内容时, go工具将自动从本地正确的位置获取代码。

Not sure where your src comes into play though... is everything in folder src ?不确定您的src在哪里发挥作用……文件夹中的所有内容都在src吗? See this post and accompanying code for a complete example you could start from.有关您可以开始的完整示例,请参阅这篇文章和随附的代码

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

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