英文:
Golang vendor package with vendored sub-packages
问题
情况:
假设我有一个名为mypackage
的包。它公开了一个名为Build()
的方法,该方法返回一个由第三方库thirdpartypackage
公开的具体结构体,例如以下示例所示:
package mypackage
import tpp "github.com/thirdpartycompany/thirdpartypackage"
func Build() *tpp.SharedStruct {
//...实现
}
公开SharedStruct
的包被放置在mypackage
的供应商目录中,因为我想分发它并使该包独立。
问题:
在将mypackage
导入到另一个项目并在我的(集成)测试中使用thirdpartypackage
时,我遇到了以下错误:
cannot use XXXX (type "github.com/mycompany/mymainproject/vendor/github.com/mycompany/mypackage/vendor/github.com/thirdcompany/thirdpartypackage-go".Token) as type "github.com/empatica/mycompany/vendor/github.com/thirdcompany/thirdpartypackage"
基本上,编译器区分了我库中的供应商第三方包和我放在主项目中的供应商包。
问题:
有没有解决此问题的方法,而不是像这里建议的那样从我的库中删除供应商依赖项?我是否遗漏了什么?
英文:
Situation:
Let's assume I've a package called mypackage
. It exposes one method called Build()
, that returns a concrete struct exposed by a third party library called thirdpartypackage
, e.g. like shown as follows:
package mypackage
import tpp "github.com/thirdpartycompany/thirdpartypackage"
func Build() *tpp.SharedStruct{
//...implementation
}
The package that exposes SharedStruct
is vendored inside mypackage
, because I want to distribute it and make that package independent.
Problem:
After importing mypackage
into another project and using thirdpartypackage
in my (integration) tests, I'm getting the following error:
> cannot use XXXX (type
> "github.com/mycompany/mymainproject/vendor/github.com/mycompany/mypackage/vendor/github.com/thirdcompany/thirdpartypackage-go".Token)
> as type
> "github.com/empatica/mycompany/vendor/github.com/thirdcompany/thirdpartypackage"
Basically the compiler is distinguishing the vendored third-party package inside my library and the vendored package that I put in my main project.
Question:
Are there ways to solve this issue, that are not removing the vendored dependencies from my library, like suggested here? Am I missing something?
答案1
得分: 3
我认为你所忽略的是这一点:
> 在名为"vendor"的目录下的代码只能被以"vendor"的父目录为根的目录树中的代码导入,而且只能使用省略了前缀(包括"vendor"元素)的导入路径。(go命令)
根据你的问题,我理解的是你的mypackage
有一个名为vendor
的子目录,所以当它被同级的其他包导入时,由于明显的原因,它们无法看到那个被供应的列表。
你可以将其理解为类似于"protected"功能。
因此,如果你想在其他包中使用该版本,那么这个vendor
目录应该与它们处于同一级别。例如:
ParentDirectory
> mypackage
> otherpackage(将导入mypackage)
> vendor(vendor应该在这里,这样ParentDirectory下的所有子目录都可以访问它)
我希望我的观点清楚明白。
英文:
I think what you are missing is this point:
>Code below a directory named "vendor" is importable only by code in the directory tree rooted at the parent of "vendor", and only using an import path that omits the prefix up to and including the vendor element. (go command)
What I understand from your problem is your mypackage
has vendor as its child, so when it's imported by any other package at same level, then for obvious reason they cannot see that vendored list.
You can understand this as being similar to a protected
feature.
So if you want to use that version in any other package, then this vendor
should be present at same level. For example
ParentDirectory
> mypackage
> otherpackage (this will be importing mypackage)
> vendor (vendor should be present in here so that all children under ParentDirectory can access this)
I hope I am clear with my point.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论