Array value types in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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