天天看点

Golang Cannot use ss(type AAA) as type AAA in map index

问题出现的背景:

今天对接别人api的时候,对方引用了kubernetes下的​​​client-go​​,具体信息如下:

ImportPath": "k8s.io/client-go/pkg/api/v1",
            "Comment": "v4.0.0-beta.0",
            "Rev": "df46f7f13b3da19b90b8b4f0d18b8adc6fbf28dc"}      

为了使用对方代码中的struct,我也下载并放置到相应的位置,但仍然报错,具体错误信息如下:

Cannot use ss(type k8sClient.ResourceName) as type ResourceName in map index )

Reports invalid index and slice expressions.

Golang Cannot use ss(type AAA) as type AAA in map index

TenantStatus、ResourceList、ResourceName定义如下:

type TenantStatus struct {
    Phase      TenantPhase     `json:"phase"`
    ActualUsed v1.ResourceList `json:"actualUsed"`
    Used       v1.ResourceList `json:"used"`
    Hard       v1.ResourceList `json:"hard"`
}
// ResourceList is a set of (resource name, quantity) pairs.
type ResourceList map[ResourceName]resource.Quantity

// ResourceName is the name identifying various resources in a ResourceList.
type ResourceName      

从截图可以看出idea对于我本地的ResourceName,并不识别

list.Status.Hard[ss].Add(item.Status.Hard["requests.cpu"])      

通过goland进入list.Status.Hard具体定义可以发现,此处引用是对方vendor中的client-go

Golang Cannot use ss(type AAA) as type AAA in map index
//k8sClient "k8s.io/client-go/pkg/api/v1"
var      

通过goland进入k8sClient.ResourceName具体定义可以发现,此处引用是我本地的client-go

Golang Cannot use ss(type AAA) as type AAA in map index

于是,决定删除对方项目vendor中的client-go包,问题解决。

Golang Cannot use ss(type AAA) as type AAA in map index

个人微信公众号: