范围内的表现

When ranging over an array, two values are returned for each iteration. The first is the index, and the second is a copy of the element at that index.

Here's my code:

var myArray = [5]int {1,2,3,4,5}
sum := 0
// first with copy
for _, value := range myArray {
    sum += value
}
// second without copy
for i := range myArray {
    sum += myArray[i]
}

Which one should i use for better performance?

Is there any difference for built-in types in these two pieces of code?

the second one is faster but the difference is too low which you can ignore

the main difference is when you have a big size loop. in that case first loop takes more memory than the second one