I am trying to complete the Exercise: Slices
from the Go Tour.
However I don't really understand what I'm being asked to do.
Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values.
I have the following code
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
a := make([][]uint8, dy)
return a
}
func main() {
pic.Show(Pic)
}
I have created the slice of length dy so far. However I don't understand the next step. Do I need to create a for loop in which I assign every element of my slice to a value in the range of dx? I don't ask for code but rather for an explanation/clarification
Do I need to create a for loop in which I assign every element of my slice to a value in the range of dx?
Yes:
[]uint8
slice of dx
size,y
' for each element a[x]
(which is a []uint8
) in order to assign a[x][y]
with the asked value (ie, one of the "interesting functions" like x^y
, (x+y)/2
, and x*y
).x
and y
are indexes in a range over a slice (a
, then a[x]
): see "For
statements".I like (x ^ y) * (x ^ y)
:
x*x + y*y
is another good one
Waqas Ilyas suggests in the comments: y * 10000 / (x + 1)
EdvardM suggests in the comments: (x+y)/2 * (x^y)
:
Chubby Cows proposes in the comments: ((x - y) * -1) * (x + y) / 2
: