Contents

Go Tutorial Part 5 Slices and Arrays

Contents
  1. Array: Fixed Length list
  2. Slice: An array that grow and shrink
  3. 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"
}
  1. []string : define a slice of type string
  2. {"Ace of Diamond", newCard()}: initialize with values
  3. append(cards, "Six of Spades"): append a new value to the slice and returns a new slice
  4. Slices index start with 0
  5. indexing: slice[0]
  6. indexing: slices[startIndexIncluding: upToNotIncluding]
  7. ignore the startIndexIncluding to start from start
  8. ignore the upToNotIncluding to go till the end