Go Tutorial Part 5 Slices and Arrays
Contents
- Array: Fixed Length list
- Slice: An array that grow and shrink
- Every single record must have the same type
package main
import "fmt"
func main() {
cards := []string{"Ace of Diamond", newCard()}
cards = append(cards, "Six of Spades")
fmt.Println(cards)
}
func newCard() string {
return "Five of Diamonds"
}
[]string
: define a slice of type string{"Ace of Diamond", newCard()}
: initialize with valuesappend(cards, "Six of Spades")
: append a new value to the slice and returns a new slice- Slices index start with 0
- indexing:
slice[0]
- indexing:
slices[startIndexIncluding: upToNotIncluding]
- ignore the
startIndexIncluding
to start from start - ignore the
upToNotIncluding
to go till the end