Arrays
- Fixed length
- Same type
- Indexable
- Contiguous in Memory
Initialising arrays
var intArr [3]int32
- Default values are set to the default values of the array type
- Indexing and slicing works similar to python,
intArr[0]andintArr[1:3] - We can also change the value at an index using
intArr[0] = 123 &intArr[0]to access the memory location of the value, and each array element is stored 4 places awayvar intArr [3]int32 = [3]int32{1,2,3}also initialises a new arrayintArr := [...]int32{1,2,3}also works
Slices
- Are arrays where we can add elements to it, not fixed length
var intSlice []int32 = []int32{4,5,6}
Methods
- cap(intSlice) ⇒ gets the capacity of the slice
- len(intSlice) ⇒ gets the length of the slice (number of elements)
- append(intSlice, 7)
- append(intSlice, intSlice2…) to append multiple values
- make(int32[] , 3, 8) ⇒ create a slice with specified length and capacity, good for if we know that we do not need to resize the slice