GoLang Array Declaration

In this tutorial, we will learn how to declare an array and different ways of arrays declaration.

In the Go Language, Array is a one-dimensional data structure that store multiple values with similar datatype.

Array Declaration

In Go language, we can declare an array by using two ways.

  1. Using var keyword
  2. Using :=

1. Array declaration using var keyword:

We need to specify the the datatype and name of the array after var keyword. If we want to specify the length, you can specify otherwise you can specify as ...

Syntax:

var array_name = [...]datatype{element1,element2,...}

(or)


var array_name = [length]datatype{element1,element2,...}

Example: -

Let us create two arrays with integer type that hold 5 values with and without specifying length.

CopiedCopy Code

package main
import ("fmt")

func main() {
    // Declare first array with 5 values (By specifying length).
  var first = [5]int{10,20,30,40,50}
  // Declare second array with 5 values (without specifying length).
  var second = []int{10,20,30,40,50}

//Display arrays
  fmt.Println(first)
  fmt.Println(second)
}

Output:

CopiedCopy Code

[10 20 30 40 50]
[10 20 30 40 50]

2. Array declaration using :=

We need to specify the datatype and name of the array. If we want to specify the length, you can specify otherwise you can specify as ...

Syntax:

array_name = [...]datatype{element1,element2,...}

(or)

array_name = [length]datatype{element1,element2,...}

Example 1: -

Let us create two arrays with integer type that hold 5 values with and without specifying length.

CopiedCopy Code

package main
import ("fmt")

func main() {
    // Declare first array with 5 values (By specifying length).
   first := [5]int{10,20,30,40,50}
  // Declare second array with 5 values (without specifying length).
  second := []int{10,20,30,40,50}

//Display arrays
  fmt.Println(first)
  fmt.Println(second)
}

Output:

CopiedCopy Code

[10 20 30 40 50]
[10 20 30 40 50]

Example 2: -

Let us create an array that hold 3 strings.

CopiedCopy Code

package main
import ("fmt")

func main() {
    // Decalre first array with 3 strings
   first := [3]string{"Welcome","to","GKINDEX"}
   
//Display array
  fmt.Println(first)
}

Output:

CopiedCopy Code

[Welcome to GKINDEX]

In Go language, it can be possible to add value at index position in an array.

Syntax:

var array_name = [...]datatype{index:element,...}

Example 1: -

Let us create an array with integer type and 3 values.

  1. Add value-60 at index-1
  2. Add value-70 at index-2
  3. Add value-80 at index-0
CopiedCopy Code

package main
import ("fmt")

func main() {
    // Decalre an array that hold three values.
   first := [...]int{1:60,2:70,0:80}
   
//Display array
  fmt.Println(first)
}

Output:

CopiedCopy Code

[80 60 70]

Explanation for the above output:

We can see that 80 is placed at first position, 60 is placed at second position and 70 is placed at last position in the array.

Example 2:-

Let us create an array with string type and 4 strings.

  1. Add "He" at index-0
  2. Add "ll" at index-1
  3. Add "o" at index-2
  4. Add "gkindex" at index-4.
CopiedCopy Code

package main
import ("fmt")

func main() {
    // Declare an array that hold 4 strings.
   first := [...]string{0:"He",1:"ll",2:"o",3:"gkindex"}
   
//Display array
  fmt.Println(first)


}

Output:

CopiedCopy Code

[He ll o gkindex]

Conclusion

Now we know how to declare array in Golang two approaches and understood the implementation with working examples.