2D slices example from https://go.dev/doc/effective_go

97 views
Skip to first unread message

Dan Ion

unread,
Aug 24, 2022, 3:35:52 PM8/24/22
to golang-dev
The 2D slices example from https://go.dev/doc/effective_go#two_dimensional_slices is confusing, isn't it?

This is the example:

// Allocate the top-level slice. 
picture := make([][]uint8, YSize) 
// One row per unit of y. 
// Loop over the rows, allocating the slice for each row. 
for i := range picture {
    picture[i] = make([]uint8, XSize)
}

This implementation assumes that we access one element by (y, x) pair, which is confusing. That's because the pair is (x, y).

Am I wrong here?


Jan Mercl

unread,
Aug 24, 2022, 3:43:51 PM8/24/22
to Dan Ion, golang-dev
Reply all
Reply to author
Forward
0 new messages