Created
August 9, 2021 13:56
-
-
Save Yapcheekian/3544c884053d68a9cd49eacb95de2570 to your computer and use it in GitHub Desktop.
Notes on go slices
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
var arrNum [5]int // this is an array | |
var sliceNum []int // this is a slice | |
fmt.Println(arrNum) // [5]int{0,0,0,0,0} array have initial value of zero value | |
fmt.Println(sliceNum) // []int(nil) slice have initial value of nil value | |
sliceNum == nil // true | |
arrNum[0] = 1 // ok | |
sliceNum[0] = 1 // compile time error because you cannot assign values to nil | |
var arrNum2 [3]int | |
arrNum = arrNum2 // compile time error because length is part of array type | |
sliceNum := []int{0,0} | |
sliceNum2 := []int{1,2,3} | |
sliceNum = sliceNum2 // ok, you can assign slice of diffrent length to a slice | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment