go mod是否要求您拉取整个模块,包括传递依赖项?

huangapple go评论75阅读模式
英文:

Does go mod require you to pull an entire module, including transitive dependencies?

问题

如果我有一个简单的导入,例如:

import (
    "hello.io/example/client"
)

...我应该假设go mod tidy会尝试“导入”(或更准确地说是传递解析)hello.io/example/下的所有内容吗?

问题详情

在我的情况下,我只需要从上面显示的example/client目录中导入几个文件。有没有一种简单的方法来做到这一点?我有一些想法:

  • hello.io/example/clienthello.io/example/server创建单独的git仓库。这显然有一些缺点。
  • 为example/client和example/server创建单独的go.mod文件。
  • 使用某种go mod的魔法告诉go mod“只关注从import语句中引用的目录中导入的内容”。

我更喜欢第三个选项,但是我在go mod文档中没有找到关于是否允许从外部项目导入子文件夹的任何信息。

问题陈述摘要

因此,总结一下,更详细版本的这个问题是:*go mod tidy是否有能力只导入依赖的某些部分,还是它是一个全盘操作?如果是后者,那么在仅依赖于monorepo中的几个文件时,划分依赖关系的最佳方法是什么?*我还将这个问题标记为“kubernetes”,因为它特别涉及到这里的更详细的示例:https://github.com/kubernetes-sigs/kpng/issues/58。

英文:

If I have a simple import such as:

import (
     "hello.io/example/client"
)

... should I assume that go mod tidy by definition, will try to “import” (or more precisely transitively resolve) all of

hello.io/example/

?

Details

In my case, I only need to import a few files from the example/client directory shown above. Is there a straight forward way to do this? Some ideas I had:

  • make a separate git repo for hello.io/example/client and hello.io/example/server. This obviously has drawbacks.
  • make a separate go.mod for example/client and example/server.
  • use some kind of go mod magic to tell go mod "only worry about importing things from the directory referenced in the import clause.

I would prefer the 3rd option, but I don't see anything in go mod documentation about wether or not importing a subfolder from an external project is allowed.

Summary of the problem statement

So to summarize, the more detailed version of this question is does go mod tidy have the ability to only import certain parts of a dependency, or is it an all or nothing operation, and if the latter, what is the best way to carve up dependencies so as not to "import the world" when simply depending on a few files in a monorepo ?.

I've also tagged this question as "kubernetes" because, specifically, it pertains to the more detailed example here: https://github.com/kubernetes-sigs/kpng/issues/58 .

答案1

得分: 3

go mod tidy命令没有只导入依赖的某些部分的能力。要么是依赖项,那么就会使用该包,要么不是依赖项。请注意,对于Go来说,文件没有实际意义,不能被导入、使用、测试、分发等(除非作为其包的一部分)。

英文:

> does go mod tidy have the ability to only import certain parts of a dependency [?]

No. It's either a dependency in which case the package is used or it's not a dependency. Note that files have no real meaning for Go and cannot be imported, used, tested, distributed, etc. (except as part of their package).

huangapple
  • 本文由 发表于 2021年9月25日 01:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/69318744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定