Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Last active January 26, 2019 00:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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