[Golang] Array

2021-10-18 hit count image

Let's see how to define and use Array in Golang.

Outline

In this blog post, I will show you how to define and use the array in Golang. You can see the full source code of this blog post on the link below.

Array

In Golang, the array is a type for storing the same type of data in contiguous memory. You can define the array like the below.

var VARIABLE_NAME [SIZE]TYPE
var a [5]int

If you define the array like the below, the 5 size, int type of the array is created. If you don’t initialize the array, the 0 that is int’s default value is assigned.

var a [5]int

When you define the array, you can only use the constant for the array size. If you use a variable for the size, the compile error occurs.

const LEN = 5
var a = [LEN]int

// Error
len := 5
var b := [len]int

To check these, create the main.go file and modify it like the below.

package main

import "fmt"

func main() {
  var a [5]int
  fmt.Println(a)

  const LEN = 5
  var b [LEN]int
  fmt.Println(b)

  // ERROR
  // var len = 5
  // var c [len]int
  // fmt.Println(c)
}

When you execute the code above, you can see the result like the below.

# go run main.go
[0 0 0 0 0]
[0 0 0 0 0]

Initial value

In Golang, you can define the array with the initial value like the below.

var a [5]int = [5]int{1, 2, 3, 4, 5}
// [1 2 3 4 5]
var a = [5]int{1, 2, 3, 4, 5}
// [1 2 3 4 5]

The array is also the variable definition, so you can use the short variable declaration(:=).

days := [3]string{"Mon", "Tue", "Wed"}
// [Mon Tue Wed]

You can initialize a part of the array, and the part of the array that is not initialized is assigned with the default of the variable type.

a := [5]int{1, 2}
// [1 2 0 0 0]

Also, you can initialize the specific position of the array like the below.

a := [5]int{1: 10, 3: 30}
// [0 10 0 30 0]

If you define the array like the below, the array size will be the same as the initialization variable size.

a := [...]int{1, 2, 3, 4, 5}
// [5]int
// [1 2 3 4 5]

If you define the variable like the below, the slice that the size is not fixed will be created. (the slice is not an array.)

a := []int{1, 2, 3, 4, 5}
// []int
// [1 2 3 4 5]

To check these, modify the main.go file like the below.

package main

import "fmt"

func main() {
  var a [5]int = [5]int{1, 2, 3, 4, 5}
  fmt.Println(a)

  var b = [5]int{1, 2, 3, 4, 5}
  fmt.Println(b)

  days := [3]string{"Mon", "Tue", "Wed"}
  fmt.Println(days)

  e := [5]int{1, 2}
  fmt.Println(e)

  f := [5]int{1: 10, 3: 30}
  fmt.Println(f)

  g := [...]int{1, 2, 3, 4, 5}
  fmt.Printf("%T\n", g)
  fmt.Println(g)

  h := []int{1, 2, 3, 4, 5}
  fmt.Printf("%T\n", h)
  fmt.Println(h)
}

When you execute the code, you can see the result like the below.

[1 2 3 4 5]
[1 2 3 4 5]
[Mon Tue Wed]
[1 2 0 0 0]
[0 10 0 30 0]
[5]int
[1 2 3 4 5]
[]int
[1 2 3 4 5]

Copy array

Copying the array is different from copying the normal variable.

a := [...]int{1, 2, 3, 4, 5}
b := [...]int{10, 20, 30, 40, 50}

fmt.Println(a)
fmt.Println(b)

b = a
fmt.Println(a)
fmt.Println(b)

As you see the example, we use the following code to copy the array.

b = a

Copying the array is copying the a memory size to b. the array stores data in contiguous memory, so when you copy a to b, the entire memory is copied instead of copying each value individually.

So if the arrays have different sizes, you can’t copy them. In Golang, by default, both sides must have the same type for operating. Therefore, an array of type [3]int cannot be copied to an array of [5]int as follows.

c := [3]int{1, 2, 3}
d := [5]int{10, 20, 30, 40, 50}

// ERROR
d = c
fmt.Println(c)
fmt.Println(d)

Multidimensional array

In Golang, you can define the multidimensional array.

var a [2][5]int

the usages are the same as the basic array. You can define the array with the initial value like the below.

package main

import "fmt"

func main() {
  a := [2][5]int{
    {1, 2, 3, 4, 5},
    {10, 20, 30, 40, 50},
  }

  for _, arr := range a {
    for _, v := range arr {
      fmt.Print(v, " ")
    }
    fmt.Println()
  }
}

When you execute the code above, you can see the result like the below.

# go run main.go
1 2 3 4 5
10 20 30 40 50

Completed

Done! we’ve seen how to define the array and how to use the array in Golang. Also, to copy the array, the same type and size are needed.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts