Slice of slices types

前端 未结 2 1618
無奈伤痛
無奈伤痛 2021-02-01 12:11

I\'m currently working my way through the excellent Tour of Go. I finished one of the exercises (#45) with the following solution:

func Pic(dx, dy int) [][]uint8         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 12:38

    To be explicit, we can use parentheses to rewrite [][]uint8 as []([]uint8): a slice of (slices of type uint8).

    Using the make built-in function, for a slice of type T, make(T, n) returns a slice of type T with length n and capacity n.

    Therefore, make([][]uint8, 2) is equivalent to make([]([]uint8), 2), it returns a slice, with length and capacity of 2, of slices of type uint8, where each slice of type uint8 is initialized to its zero value (a nil reference with a length and capacity of zero).

    Multi-dimensional slices are jagged and are analogous to multi-dimensional jagged arrays.

    For example,

    package main
    
    import "fmt"
    
    func main() {
        ss := make([][]uint8, 2) // ss is []([]uint8)
        fmt.Printf("ss:    %T %v %d\n", ss, ss, len(ss))
        for i, s := range ss { // s is []uint8
            fmt.Printf("ss[%d]: %T %v %d\n", i, s, s, len(s))
        }
    }
    

    Output:

    ss:    [][]uint8 [[] []] 2
    ss[0]: []uint8 [] 0
    ss[1]: []uint8 [] 0
    

提交回复
热议问题