我正在尝试通过使用在另一篇文章Golang create a slice of maps上看到的方法,来创建map[string]float64
的切片。
我试图将其更改为map [string] interface {},但随后提示
cannot use distmap (type map[string]float64) as type map[string]interface {} in append
我是Go的新手。我以为interface {}可以保留任何值,为什么在这种情况下不能容纳float64?
minDistance := make([]map[string]interface{}, 0, factorial(len(cities)))
for _, city := range cities {
for k, v := range distances {
var c []string
var d []float64
if strings.HasPrefix(k, city) {
c = append(c, strings.Split(k, "+")[1])
d = append(d, v)
}
var min float64
index := 0
if units == "mi" {
min = 12450.5
} else {
min = 20037.5
}
for i, dist := range d {
if dist < min {
min = dist
index = i
}
}
distmap := map[string]float64{city + "+" + c[index]: min}
minDistance = append(minDistance, distmap)
}
// fmt.Printf("Distance between %s: %f %s\n", k, v, units)
}
编译时出错:
.\main.go:151:42: []map[string]float64 literal is not a type