Answer the question
In order to leave comments, you need to log in
How to understand the task condition https://go-tour-en-us.appspot.com/moretypes/18?
How to understand the task condition https://go-tour-en-us.appspot.com/moretypes/18 ?
I wrote the function like this.
package main
import "golang.org/x/tour/pic"
array = [][]uint8
func Pic(dx, dy int) [][]uint8 {
for i:=0; i < dy; i++ {
array[i] = dx
}
return array
}
func main() {
pic.Show(Pic)
}
Answer the question
In order to leave comments, you need to log in
Return a two-dimensional array uint8, where the values are colors (a number from 0 to 255). For example like this:
func Pic(dx, dy int) [][]uint8 {
result := make([][]uint8, dy)
for i := range result {
result[i] = make([]uint8, dx)
}
for i := range result {
for j := range result[i] {
result[i][j] = uint8((i + j) / 2)
}
}
return result
}
You need to create a slice of slices (two-dimensional array) of the given size.
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
array := make([][]uint8, dy)
for y := 0; y < dy; y++ {
array[y] = make([]uint8, dx)
for x := 0; x < dx; x++ {
pixel := uint8((x + y) / 2)
array[y][x] = pixel
}
}
return array
}
func main() {
pic.Show(Pic)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question