HomeAuthorsContact
GO and Arrays

GO and Arrays

By Frédéric Schmidt
May 21, 2020
1 min read

Presentation of arrays

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.

Allocation syntax

1 dimension

var <NOM VARIABLE>[<TAILLE>]<TYPE>

n dimensions

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)

Assignment syntax

<NOM VARIABLE>[<INDEX>] = <VALEUR>

Principle

It should be noted that when allocating:

  • The allocation of elements is contiguous;
  • Content is always initialized
  • That the starting index is 0;
  • The table is immutable and therefore to enlarge the table you must create another one.

Example

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 }
}

In Brief

And now you know:

  • Declare any type of arrays;
  • Assign values.

Tags

#ProgramingLanguages#go#goland#array
Frédéric Schmidt

Frédéric Schmidt

Software Architect

I'm Software Developer & Architect. I'm more than 20 years experiences in differents IT service companies and software editors. I like studies some new technologies and share this passion altroughts my technical blog.

Expertise

Architecture
Development
Modelisation
Technical writer

Social Media

githublinkedin

Related Posts

GO and Packages
Programming Languages
GO and Packages
June 09, 2020
1 min
© 2022, All Rights Reserved.

Quick Links

Contact Us

About me

Social Media