Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go language: Type XXX is not an expression

Tags:

slice

go

I have written a function:

func Pic(dx, dy int) [][]uint8 {
    type matrix [][]uint8 

    for i := 0; i < dx; i++ { // fills up the matrix with z's in their right places.
        for j := 0; j < dy; j++ {
            matrix[i][j] = Z(i,j)
        }
    }

    return matrix
}

that is supposed to fill up a matrix with z values for each x and y value and return it. As I want to have different dimensions for the matrix depending of parameters to the Pic function, I create a slice i line 2. Then in my for loops i fill the matrix up.

I get an error upon running this code: type matrix is not an expression for the matrix[i][j] = Z(i,j) line. What am I doing wrong? Should matrix[i][j] evaluate to an expression? Why should it, when I want to put something there (it's empty/non-existent now!) ?

like image 585
Sahand Avatar asked Mar 30 '16 16:03

Sahand


1 Answers

You're declaring matrix as a type, but using it as a variable.

try:

var matrix [][]uint8
like image 198
JimB Avatar answered Sep 23 '22 22:09

JimB