Control Flow
Range Loops
Use range to visit every value in a slice.
Range Loops
range_loop.go
package main
import "fmt"
func main() {
var scores =
best := scores[0]
for _, score := range scores {
if score > best {
best = score
}
}
fmt.Println("scores=", scores)
fmt.Println("best=", best)
}
package main
import "fmt"
func main() {
var scores =
best := scores[0]
for _, score := range scores {
if score > best {
best = score
}
}
fmt.Println("scores=", scores)
fmt.Println("best=", best)
}
package main
import "fmt"
func main() {
var scores =
best := scores[0]
for _, score := range scores {
if score > best {
best = score
}
}
fmt.Println("scores=", scores)
fmt.Println("best=", best)
}
range loop
`range` gives an index and value for each element in a slice.