I'm starting with Go (or Golang), and I wanted to know how to get the size of a matrix, for example of type `[][]uint8`

.

I tried using `len`

, but it gives me an error.

3

I'm starting with Go (or Golang), and I wanted to know how to get the size of a matrix, for example of type `[][]uint8`

.

I tried using `len`

, but it gives me an error.

asked by Aaron 28.12.2016 в 00:53

source
4

In Go, all the fixes that are created have a defined length and this is part of its type:

```
var array [10]uint8
var arrayOfArrays [7][3]uint8
```

In the first case, the variable `array`

is an array of 10 elements of type `uint8`

and in the second the variable `arrayOfArrays`

is an array of 7 elements where each element is an array of 3 elements of type `uint8`

That statement is equivalent to:

```
var arrayOfArrays [7]([3]uint8)
```

Now, Go has a built-in function called `len()`

that returns an integer equivalent to the length of the object passed to it as a parameter, if the object is an array it returns the number of elements in that array. That means that:

```
len(array)
```

returns the number of array elements `array`

(10). For arranging fixes `arrayOfArrays`

(or the 'array', as you call it):

```
var filas = len(arrayOfArrays)
```

will return 7, because this variable is an array of 7 elements, where each element is also another arrangement, and

```
var columnas = len(arrayOfArrays[0])
```

will return 3, in this case the returned value is the number of elements of the first array that is itself an array element `arraysOfArrays`

. If the objective is to know how many elements the arrangement of arrays has in total, the value is the multiplication of the variables rows and columns. Unlike other languages, in Go it is not possible to create an array of elements that are arrays of different sizes, so you can be sure that the number of elements [the length] of an array of arrays is the multiplication of the number of rows and columns.

answered by
28.12.2016 в 03:58

0

`var a [5][3]int;`

// declaration of the matrix

`len(a);`

// size of the matrix columns

`len(a[0]);`

// size of the rows of the matrix

answered by
28.12.2016 в 00:58