go语言学习-笔记

1) go的数组不同于c的数组, go的array更像c的struct, 传递是,是整个copy的操作.

Go's arrays are values. An array variable denotes the entire array; it is not a pointer to the first array element (as would be the case in C). This means that when you assign or pass around an array value you will make a copy of its contents. (To avoid the copy you could pass a pointer to the array, but then that's a pointer to an array, not an array.) One way to think about arrays is as a sort of struct but with indexed rather than named fields: a fixed-size composite value.

 

2) slice 内存分布

b := []byte{'g', 'o', 'l', 'a', 'n', 'g'}
// b[1:4] == []byte{'o', 'l', 'a'}, sharing the same storage as b

a := b[1:4], when the values of b is changed, a aloso be changed.

 

3) go tour pic :interesting!

 

4) 代码中 image 申请的内存 何时释放、如何释放?

func pic2(dx, dy int) [][]uint8 {
        image := make([][]uint8, dy)
        for i := 0; i < dy; i++ {
                image[i] = make([]uint8, dx)
        }
        for i:= 0; i < dy; i++ {
                for j := 0; j < dx; j++ {
                        //image[i][j] = (uint8)(math.Sin(float64(i+j)/180*math.Pi))
                        //image[i][j] = (uint8)(i*j)
                        //image[i][j] = (uint8)(math.Pow(float64(i),float64(j)))
                        //image[i][j] = (uint8)(math.Sin(float64(i*j)/180*math.Pi))
                        image[i][j] = uint8((i+j)/4)
                }
        }
        return image
}

func main() {
        pic.Show(pic2)
}

  

posted @ 2020-03-22 15:08  upupon  阅读(119)  评论(0编辑  收藏  举报