
In almost all languages there is this notion of an array for data storage. The GO language does not cut into it either.
Simply put, an array is a finite sequence of elements of the same type.
var <NOM VARIABLE>[<TAILLE>]<TYPE>
var <NOM VARIABLE>[<TAILLE1>][<TAILLE2>]...[<TAILLEn>]<TYPE>
Reminder: In terms of the visibility of a function, in this sense it is public or private. This is done by either putting the first letter in lowercase for a private function and a capital letter for a public service.
Note: The size of the array must be an integer greater than or equal to zero (even if zero will represent an array with no element).
Note: be careful of the number of dimensions because you can exceed the memory space but the compiler will let you know, as well as the IDE (for visual code for example)
<NOM VARIABLE>[<INDEX>] = <VALEUR>
It should be noted that when allocating:
Allocation of an array of 4 strings.
package main import "fmt" func main() { var tableau [4]int fmt.Printf("tableau %v", tableau) // Résulat : tableau [0 0 0 0]
}
package main import "fmt" func main() { var tableau [4]int tableau[0] = 99 tableau[3] = 100 fmt.Printf("tableau %v", tableau) // Résulat : tableau [99 0 0 100] } package main import "fmt" func main() { var tableau [4]int tableau[0] = 99 tableau[3] = 100 fmt.Printf("tableau %v de longueur = %d", tableau, len(tableau)) // Résulat : tableau [99 0 0 100] de longueur = 4 } package main import "fmt" func main() { var vectors [3]struct{ x, y int } vectors[1].x = 100 vectors[1].y = 150 fmt.Printf("tableau: %v de longueur: %d, de type: %T", vectors, len(vectors), vectors) // Résulat : tableau: [{0 0} {100 150} {0 0}] de longueur: 3, de type: [3]struct { x int; y int } } package main import "fmt" func main() { var mat_vect [3][3]struct{ x, y int } mat_vect[1][1].x = 100 mat_vect[1][1].y = 150 fmt.Printf("tableau: %v de longueur: %d, de type: %T", mat_vect, len(mat_vect), mat_vect) // Résulat : tableau: [[{0 0} {0 0} {0 0}] [{0 0} {100 150} {0 0}] [{0 0} {0 0} {0 0}]] // de longueur: 3, de type: [3][3]struct { x int; y int } }
And now you know: