gpt4 book ai didi

带有销售子包的 Golang vendor 包

转载 作者:IT王子 更新时间:2023-10-29 02:03:44 25 4
gpt4 key购买 nike

情况:

假设我有一个名为 mypackage 的包。它公开了一个名为 Build() 的方法,该方法返回一个由名为 thirdpartypackage 的第三方库公开的具体结构,例如如下图所示:

package mypackage

import tpp "github.com/thirdpartycompany/thirdpartypackage"

func Build() *tpp.SharedStruct{
//...implementation
}

公开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"

基本上,编译器会区分我的库中出售的第三方包和我放在主项目中的出售的包。

问题:

有没有解决这个问题的方法,而不是像建议的那样从我的库中删除销售依赖项 here ?我错过了什么吗?

最佳答案

我认为你缺少的是这一点:

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)

我从你的问题中了解到你的 mypackage 有 vendor 作为它的 child ,所以当它被同一级别的任何其他包导入时,出于明显的原因,他们看不到那个 vendored 列表。

您可以将其理解为类似于protected 功能。

因此,如果您想在任何其他包中使用该版本,则该 vendor 应该出现在同一级别。例如

ParentDirectory
> mypackage
> otherpackage (this will be importing mypackage)
> vendor (vendor should be present in here so that all children under ParentDirectory can access this)

我希望我的观点很清楚。

关于带有销售子包的 Golang vendor 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com