【发布时间】:2011-12-03 22:23:08
【问题描述】:
我目前正在努力完成出色的Tour of Go。 我使用以下解决方案完成了一项练习(#45):
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy) /* type declaration */
for i := range pic {
pic[i] = make([]uint8, dx) /* again the type? */
for j := range pic[i] {
pic[i][j] = uint8((i+j)/2)
}
}
return pic
}
我不明白为什么我必须使用带有uint8 类型的make 语句两次(参见sn-p 中的cmets)。这似乎是多余的,但我无法弄清楚如何以其他方式做到这一点。
【问题讨论】:
-
可以说,可以在开头给
pic提供dx*dy的容量,第二行是pic := make([][]uint8, dy, dx*dy)。通过一次分配所需的内存来提高性能吗?