×

go · level 1 · unit 6 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Walk through the slice and check every item.

To a slice, walk through it and check each item with an if. Picture looking for your friend in a crowd, one face at a time.

Keep a : a bool box that starts as false. When you spot a match, set it to true. You can also break there, since there is no need to keep looking.

The same walk finds the biggest item. Start with the first item as the best so far. Whenever an item beats it, that item becomes the new best.

package main
import "fmt"
func main() {
	guests := []string{"Ava", "Ben", "Cleo"}
	found := false
	for _, guest := range guests {
		if guest == "Ben" {
			found = true
		}
	}
	fmt.Println("Is Ben coming?", found)
}

To find the smallest instead, swap > for < when you compare.