天天看點

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

個人微信公衆号: