3 回答

TA貢獻2012條經驗 獲得超12個贊
用參數的類型聲明變量dss。根據需要為變量賦值。
var dss interface{}
if command == "VMFS" {
dss = &[]mo.Datastore{}
} else {
dss = &[]mo.HostSystem{}
}
err = v.Retrieve(ctx, []string{commandChoices[command]}, []string{"name", "summary"}, dss)
if err != nil {
log.Fatal(err)
}

TA貢獻1790條經驗 獲得超9個贊
你想要的是一個空接口,就像你的方法接收到的一樣。你也可以看看這個例子https://tour.golang.org/methods/14
在您的特定情況下,您想要:
var dss interface{}
if command == "VMFS"{
dss = []mo.Datasore
}else{
dss = []mo.HostSystem
}

TA貢獻1808條經驗 獲得超4個贊
A_kat 和 Muffin 對答案進行了一些更改。
你可以做:
if command == "VMFS" {
dss := []mo.Datasore{}
}else{
dss := []mo.HostSystem{}
}
err = v.Retrieve(ctx, []string{commandChoices[command]}, []string{"name", "summary"}, &dss)
if err != nil {
log.Fatal(err)
}
迭代范圍時,這不會給您錯誤dss
- 3 回答
- 0 關注
- 218 瀏覽
添加回答
舉報