golang 缺少 len 参数
golang missing len argument
如何把行做成map[string]map[string]interface{}
cannot use s.ID (type string) as type map[string]interface {} in assignment
var row = make(map[string]map[string]interface{})
列表服务器
func ListServers() (map[string]map[string]interface{}, error) {
listOptions := servers.ListOpts{}
pager := servers.List(GetClientCompute(), listOptions)
err := pager.EachPage(func(page pagination.Page) (bool, error) {
serverList, err := servers.ExtractServers(page)
if err != nil {
fmt.Println(err)
}
for _, s := range serverList {
row["ID"] = s.ID <---- error is here
row["Name"] = s.Name <---- error is here
if s.Addresses["public"] != nil {
for _, i := range s.Addresses["public"].([]interface{}) {
temp := i.(map[string]interface{})
if temp["version"].(float64) == 4 {
row["IP"] = temp["addr"]
}
}
}
t, _ := time.Parse(time.RFC3339, s.Created)
row["Flavor"] = s.Flavor
row["Created"] = time.Now().Sub(t) <---- error is here
row["Status"] = s.Status <---- error is here
}
return false, nil
})
// fmt.Println(lists)
return row, err
}
row
是 map[string]interface{}
的切片。像这样初始化切片时需要提供长度:
row := make([]map[string]interface{}, 0)
切片的索引必须是整数,这就是为什么您会遇到评论中提到的第二个问题。
假设 serverList
是一个切片。您的代码可以修改为:
rows := make([]map[string]interface{}, 0) // create a slice
// ... codes omitted
for _, s := range serverList {
row := make(map[string]interface{}) // create an item
row["ID"] = s.ID
row["Name"] = s.Name
// ... codes omitted
row["Flavor"] = s.Flavor
row["Created"] = time.Now().Sub(t)
row["Status"] = s.Status
rows = append(rows, row) // append the item to the slice
}
return rows, err
如何把行做成map[string]map[string]interface{}
cannot use s.ID (type string) as type map[string]interface {} in assignment
var row = make(map[string]map[string]interface{})
列表服务器
func ListServers() (map[string]map[string]interface{}, error) {
listOptions := servers.ListOpts{}
pager := servers.List(GetClientCompute(), listOptions)
err := pager.EachPage(func(page pagination.Page) (bool, error) {
serverList, err := servers.ExtractServers(page)
if err != nil {
fmt.Println(err)
}
for _, s := range serverList {
row["ID"] = s.ID <---- error is here
row["Name"] = s.Name <---- error is here
if s.Addresses["public"] != nil {
for _, i := range s.Addresses["public"].([]interface{}) {
temp := i.(map[string]interface{})
if temp["version"].(float64) == 4 {
row["IP"] = temp["addr"]
}
}
}
t, _ := time.Parse(time.RFC3339, s.Created)
row["Flavor"] = s.Flavor
row["Created"] = time.Now().Sub(t) <---- error is here
row["Status"] = s.Status <---- error is here
}
return false, nil
})
// fmt.Println(lists)
return row, err
}
row
是 map[string]interface{}
的切片。像这样初始化切片时需要提供长度:
row := make([]map[string]interface{}, 0)
切片的索引必须是整数,这就是为什么您会遇到评论中提到的第二个问题。
假设 serverList
是一个切片。您的代码可以修改为:
rows := make([]map[string]interface{}, 0) // create a slice
// ... codes omitted
for _, s := range serverList {
row := make(map[string]interface{}) // create an item
row["ID"] = s.ID
row["Name"] = s.Name
// ... codes omitted
row["Flavor"] = s.Flavor
row["Created"] = time.Now().Sub(t)
row["Status"] = s.Status
rows = append(rows, row) // append the item to the slice
}
return rows, err