Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Array value types in Golang
package main
import (
"fmt"
)
func changeArray(numbers [5]int) {
numbers[0] = 55 /* Passed by value */
fmt.Println("Changed numbers ::", numbers)
}
func main() {
/*Arrays are value types*/
countries := [...]string{"USA", "UK", "AU"}
mycountries := countries
mycountries[1] = "NZ" /* Change in mycountries does not change countries */
fmt.Println(countries, mycountries) /* Prints: [USA UK AU] [USA NZ AU]*/
numbers := [...]int{1, 2, 4, 5, 8}
changeArray(numbers) /* Prints: Changed numbers :: [55 2 4 5 8]*/
fmt.Println("Original numbers:", numbers) /* Original array is unchanged, Prints: Original numbers: [1 2 4 5 8] */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.