Showing posts with label go language arrays. Show all posts
Showing posts with label go language arrays. Show all posts

Thursday, 2 May 2019

Go Language: Arrays are passed by value


Unlike other languages, Array are passed by value in Go.

For example, if you call updateCountries method by passing an array as argument, the changes done for the array ‘countries’ inside the method ‘updateCountries’ is not reflected outside.

func updateCountries(countries [5]string) {
    countries[0] = "Dummy"
    countries[1] = "Dummy"
    countries[2] = "Dummy"
    countries[3] = "Dummy"
    countries[4] = "Dummy"
}

App.go
package main

import "fmt"

func main() {
    countries := [5]string{}

    countries[0] = "India"
    countries[1] = "Bangladesh"
    countries[2] = "Canada"
    countries[3] = "Austria"
    countries[4] = "Germany"

    fmt.Println(countries)

    fmt.Println("\nUpdating countries with Dummy data")

    updateCountries(countries)

    fmt.Println()
    fmt.Println(countries)
}

func updateCountries(countries [5]string) {
    countries[0] = "Dummy"
    countries[1] = "Dummy"
    countries[2] = "Dummy"
    countries[3] = "Dummy"
    countries[4] = "Dummy"
}

Output
[India Bangladesh Canada Austria Germany]

Updating countries with Dummy data

[India Bangladesh Canada Austria Germany]



Previous                                                 Next                                                 Home

Go Language: Arrays


Array is a collection that stores elements of same type.

Syntax
arrayame := [size]dataType{}

Example
countries := [5]string{}

Above statement defines an array of strings of size 5. ‘countries’ can able to store 5 strings.

HelloWorld.go
package main

import "fmt"

func main() {
 countries := [5]string{}

 countries[0] = "India"
 countries[1] = "Bangladesh"

 fmt.Println(countries)
}

Output
[India Bangladesh   ]

One problem with mentioning size to the array is, we can’t insert elements > size of the array.


HelloWorld.go
package main

import "fmt"

func main() {
 countries := [5]string{}

 countries[0] = "India"
 countries[1] = "Bangladesh"
 countries[2] = "Canada"
 countries[3] = "Austria"
 countries[4] = "Germany"

 countries[5] = "Sri lanka"

 fmt.Println(countries)
}

As you see above program, I mentioned size of array as 5, but I am trying to insert 6th element into the array.

When you run HelloWorld.go program, you will end up in below error.

$ go run HelloWorld.go
# command-line-arguments
./HelloWorld.go:14:11: invalid array index 5 (out of bounds for 5-element array)

Length of the array
You can use ‘len’ function to get the length of the array.


HelloWorld.go
package main

import "fmt"

func main() {
 countries := [5]string{}

 countries[0] = "India"
 countries[1] = "Bangladesh"

 fmt.Println("Array length : ", len(countries))
}

Output
Array length :  5

Note
Once array is defined, its length is fixed, it can't be resized.



Previous                                                 Next                                                 Home