我在创建接口数组这个简单的任务时遇到了困难。这是我的代码,
var result float64
for i := 0; i < len(diff); i++ {
result += diff[i]
}
result = 1 / (1 + math.Sqrt(result))
id1 := user1.UserId
id2 := user2.UserId
user1.Similar[id2] = [2]interface{id2, result}
user2.Similar[id1] = [2]interface{id1, result}
结果是一个浮点数,用户* .UserId是一个int。
我的错误讯息是
syntax error: name list not allowed in interface type
答案 0 :(得分:5)
例如,
package main
import (
"fmt"
)
func main() {
x, y := 1, "@"
a := [2]interface{}{x, y}
fmt.Println(a)
b := [2]interface{}{0, "x"}
fmt.Println(b)
}
输出:
[1 @]
[0 x]