3 ms·
Alternative options of solving this issue include `break`ing when the right option is found (so the loop variable doesn't get overwritten again) or moving every
by majewsky 9y ago
Alternative options of solving this issue include `break`ing when the right option is found (so the loop variable doesn't get overwritten again) or moving everything into a function:
func findByName(students []*Student, name string) *Student {
for _, s := range students {
if s.name == name {
return s
}
}
return nil
}
Both options have the added advantage of stopping iteration as soon as the result is found.