我正在测试go 1.18中的仿制药,并查看了这example个.
这是我在迭代切片时遇到的一些问题.这就是我try 的:
import "fmt"
// NumberSlice constraint
type NumberSlice interface {
[]int64 | []float64
}
func add[N NumberSlice](n N) {
// want: to range over n and print value of v
// got: cannot range over n (variable of type N constrained by NumberSlice)
// (N has no core type)
for _, v := range n {
fmt.Println(v)
}
}
func main() {
ints := []int64{1, 2}
add(ints)
}
我如何做到这一点?