【发布时间】:2016-08-20 02:27:13
【问题描述】:
我正在阅读 Golang 教程,但对于切片练习中的一些值的作用,我有点困惑。 https://tour.golang.org/moretypes/18
这是我感到困惑的代码:
值 0 是一个完美的蓝色像素,值 255 是一个完美的白色像素。所以当显示的值是某种形式的x*y 时,这里会发生什么(我做了/20 以使图像更大一点,更容易看到)。
如果您水平跟随图像,您会看到在过程中的某个时刻,不断增加的 x 和 y 值似乎恢复为蓝色(0 值)如果我在返回中键入像 256 这样的静态值我得到一个编译错误。所以它显然不允许数字 go off the scale 并恢复为 0 或任何东西。那么它是如何得到图中的蓝色曲线的呢?
此处导入源:https://github.com/golang/tour/blob/master/pic/pic.go#L15
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
//First, the array has to be made so we can put some values in it later
//This only makes the second dimension of the array ([[uint8 dy]])?
image := make([][]uint8, dy)
//The inputs into the function are Int's, so it is ok to have a non uint8
//loop initializer
for x := 0; x < dy; x++ {
//once we are in the loop we have to make the first dimension of the array
//based on the dx values
image[x] = make([]uint8, dx)
for y := 0; y < dx; y++ {
//This is a function +to assign the pixel values to the array
image[x][y] = uint8((x * y) /20)
}
}
return image
}
func main() {
pic.Show(Pic)
}
【问题讨论】:
标签: go