Home >Backend Development >Golang >golang error: 'cannot assign to range variable...' How to solve it?
Recently, when I was writing code in Golang, I encountered an error: "cannot assign to range variable...". This error made me very distressed because I didn't know how to solve it. After some exploration and research, I finally found the solution and now I share it with you.
First, let’s take a look at the scenario where this error occurs. In Golang, we usually use the range keyword to iterate elements in a container (such as arrays, slices, etc.). When we want to modify the elements during the iteration process, the error "cannot assign to range variable..." will appear. . The following is an example:
package main import "fmt" func main() { nums := []int{1, 2, 3, 4, 5} for _, num := range nums { num = num * 2 // 报错:“cannot assign to range variable num” } fmt.Println(nums) }
In this example, we want to multiply each element in nums by 2. However, an error occurred during the compilation process, "cannot assign to range variable num" ,Why is this?
The reason is that the range keyword iteration in Golang gets a copy of the element, not the element itself. Changing the copy within the loop body does not change the original element. Therefore, modifying the value of num is invalid, and an error occurs.
So how to solve this problem? There are two ways to solve this.
Method 1: Using index iteration
We can use the index in the for loop to iterate the elements in the container, and then modify the value of the original element instead of modifying the value of the copy. The following is the code implementation:
package main import "fmt" func main() { nums := []int{1, 2, 3, 4, 5} for i := range nums { nums[i] = nums[i] * 2 } fmt.Println(nums) }
In the code, we use i as the index to iterate the elements in the container nums and modify the value of nums[i]. In this way, the value of the original element can be successfully modified, and the output result is [2 4 6 8 10].
Method 2: Using pointers
We can also use pointers to elements to solve this problem. The value of the original element can be modified by passing a pointer to the iterated copy as a parameter to a function. The following is the code implementation:
package main import "fmt" func double(num *int) { *num = *num * 2 } func main() { nums := []int{1, 2, 3, 4, 5} for i, num := range nums { double(&nums[i]) } fmt.Println(nums) }
In the code, we define a double function whose parameter is a pointer to int type. In the for loop, we iterate over the elements of the container nums and pass the pointer of the iterated copy to the double function, which modifies the value of the original element and returns it. In this way, the value of the original element can be successfully modified, and the output result is [2 4 6 8 10].
To sum up, we can use two methods to solve the "cannot assign to range variable..." error in Golang, namely using index iteration and using pointers. It is necessary to choose the appropriate method to solve according to the specific business needs.
The above is the detailed content of golang error: 'cannot assign to range variable...' How to solve it?. For more information, please follow other related articles on the PHP Chinese website!