In functions, you can return directly from a loop:
func findIndex(nums []int, target int) int {
for i, n := range nums {
if n == target {
return i
}
}
return -1
}
Return exits both the loop and the function. This is cleaner than using break and a found variable when your goal is to return a value.