Go Basics
Composite Data Types
Arrays
Arrays are fixed length
ArrayType = [length]ElementType
Slices
Numbered lists of a single type. They can be resized. Slices are built on top of arrays and passed by reference
Composite literal syntax t := type{values}
Iterate through slice
Working with Slices
Make function will create a new slice
Shorthand for creating a new slice and iterating through it
Append items to slice.
1listOdd := []int{1, 3, 5, 7, 8}
2listOdd = append(listOdd, 9, 11, 13, 15)
3fmt.Println(listOdd)
4
5newOddNumbers := []int{17, 19, 21}
6listOdd = append(listOdd, newOddNumbers...)
go
...
will unfurl a list. It is useful for passing in variadic parameters from list data types (like above.)
Slicing a slice
1fmt.Println(listOdd[0])
2// 1
3fmt.Println(listOdd[1])
4// 2
5fmt.Println(listOdd[2:5])
6// [5 7 8]
7// From the beginning to the specified index
8fmt.Println(listOdd[:5])
9// [1 3 5 7 8]
10// From the specified index to the end
11fmt.Println(listOdd[5:])
12// [9 11 13 15 17 19 21]
go
Deleting from a slice
Maps
hash table data type
MAPS CAN ONLY BE INITIATED USING make()
or A LITERAL.
cannot append to a zero valued maps!