我正在瀏覽Golang教程,我對使用片練習中的一些值做些什麼感到困惑。 https://tour.golang.org/moretypes/18Golang練習片如何處理大數值
這裏是我感到困惑的代碼:
0值是完全藍色像素和255的值是一個完全白像素。因此,當顯示的值是某種形式的x*y
(我做了/20
以使圖像變大一點並更容易看清)時發生了什麼。
如果你水平地跟着圖像,你會看到在過程中的某一點,不斷增加的x和y值似乎恢復爲藍色(0值)如果我在返回I中鍵入一個像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)
}
@deltaskelta爲什麼當你輸入'uint8(256)'時會失敗?是'uint8(256)'給出'常數256溢出uint8'但是'var i int = 256 \t fmt.Println(uint8(i))'給出'0',見:https://github.com/golang/去/問題/ 16153 – 2016-08-20 05:00:45