简体   繁体   English

如何将一个 go 模块嵌套在多个 Go 模块中作为本地依赖项

[英]How to nest one go module inside multiple Go modules as local dependency

$ go version
1.13.3

I have a folder structure as follows:我的文件夹结构如下:

GOPATH
+---src
     +--- my-api-server
           +--- my-auth-server
                 +--- main.go
                 +--- go.mod
                 +--- go.sum
           +--- my-utils
                 +--- go.mod
                 +--- go.sum
                 +--- uuid
                       +--- uuid.go

my-auth-server uses my-api-server/my-utils/uuid as a dependency my-auth-server使用my-api-server/my-utils/uuid作为依赖项

I tried moving my-utils inside my-auth-server , but as a library, my-utils will be used in multiple places.我尝试将my-utils移动到my-auth-server中,但作为一个库, my-utils将在多个地方使用。

Now, my-utils also has a go.mod , but that contains a module declaration.现在, my-utils也有一个go.mod ,但它包含一个module声明。 If I place it in my-auth-server , the module path becomes my-api-server/my-auth-server/my-utils如果我将它放在my-auth-server中,模块路径将变为my-api-server/my-auth-server/my-utils

If I have 2 servers,如果我有 2 台服务器,

  • my-auth-server
  • my-session-server

I cannot place my-utils inside both because there can only be one module declaration per go.mod .我不能将my-utils放在两者中,因为每个go.mod只能有一个module声明。

So, how I use this in two different projects as submodule?那么,我如何在两个不同的项目中使用它作为子模块呢?

Any help in solving this would also be appreciated.解决此问题的任何帮助也将不胜感激。

A clean way to do achieve this is to have utils a standalone module outside of all projects and then import wherever you want.实现此目的的一种简洁方法是在所有项目之外使用 utils 一个独立模块,然后在任何您想要的地方导入。 Since its evident you want it to be a module itself.既然很明显,您希望它本身就是一个模块。

Like喜欢

GOPATH
+---src
     +--- my-api-server
           +--- my-auth-server
                 +--- main.go
                 +--- go.mod
                 +--- go.sum
     +--- my-utils
          +--- go.mod
          +--- go.sum
          +--- uuid
               +--- uuid.go

But if you still need to have utils also maintained as part of your API server then have your API server as module and import it wherever you need utils package.但是,如果您仍然需要将 utils 作为 API 服务器的一部分进行维护,那么将 API 服务器作为模块并将其导入任何需要 utils package 的地方。 This is discouraged but will not do any harm since GO optimizes as part of compilation required context.这是不鼓励的,但不会造成任何伤害,因为 GO 作为编译所需上下文的一部分进行了优化。

Like this像这样

GOPATH
+---src
     +--- my-api-server
           +--- my-auth-server
                 +--- main.go
                 +--- go.mod
                 +--- go.sum
                 +--- my-utils
                      +--- uuid
                           +--- uuid.go

What i understand is you need to maintain multi modules in single repo and cross refer.我的理解是您需要在单个仓库中维护多个模块并交叉引用。 As far as I understand this is not how its supposed to work since you can always cross refer nested package by module relative path.据我了解,这不是它应该如何工作的,因为您始终可以通过模块相对路径交叉引用嵌套的 package。

Also maybe you know this, but since you are using modules, you need to sit in GOPATH/GOROOT for resolution.也许你也知道这一点,但由于你使用的是模块,你需要坐在 GOPATH/GOROOT 中进行解析。

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

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