Contents

Go Tutorial Part 6 For Loop

Contents
package main

import "fmt"

func main() {
	cards := []string{"Ace of Diamond", newCard()}

	cards = append(cards, "Six of Spades")
	for i, card := range cards {
		fmt.Println(i, card)
	}
}

func newCard() string {
	return "Five of Diamonds"
}
  1. for: start defining a for loop
  2. index: index of the element in the array
  3. card: current card we are iterating over
  4. range cards: take the slice of card and range over

why := each time?

because each iteration throws the old variables away and mew are defined