In the Go language, slice is a powerful data structure that can easily handle continuous data blocks. Compared with arrays, slices have variable length and elements can be added or removed, making slices one of the frequently used data structures in Go.
However, when using slice, we inevitably need to modify some elements in the slice. This article will explore the modification operation of slice.
To modify the elements in the slice, you can use the index to access the elements in the slice like an array. For example, the following code demonstrates how to modify the element at a specified position in the slice:
package main import "fmt" func main() { nums := []int{1, 2, 3, 4} fmt.Println(nums) // 输出 [1 2 3 4] nums[1] = 5 fmt.Println(nums) // 输出 [1 5 3 4] }
In the above code, we first initialize a slice containing 4 integers, and then modify the value of the second element to 5. Finally, we print out the modified slice. As you can see, the modification was successful.
We can use the built-in function append() to add elements to the slice. The simplest way to modify a slice is to use the append() function. The append() function is used similar to Array.push() in JavaScript. The following code demonstrates how to add elements to a slice:
package main import "fmt" func main() { nums := []int{1, 2, 3, 4} fmt.Println(nums) // 输出 [1 2 3 4] nums = append(nums, 5) fmt.Println(nums) // 输出 [1 2 3 4 5] }
In the above code, we first initialize a slice containing 4 integers, and then use the append() function to add 5 to the end of the slice. Finally, we print out the added slice. As you can see, the addition was successful.
It should be noted that if the number of elements in the slice has reached its capacity, the underlying array may reallocate space when adding elements, which will cause performance degradation. Therefore, in order to avoid unnecessary reallocation of space when adding elements, we should first use the built-in function cap() to determine whether expansion is needed:
if len(nums)+1 > cap(nums) { newNums := make([]int, len(nums), cap(nums)*2) copy(newNums, nums) nums = newNums }
This code snippet checks whether there is enough capacity in the slice to Add a new element. If not, create a new slice with twice the capacity and use the copy() function to copy all elements from the old slice to the new slice. Finally, assign the new slice to the original slice. This approach is more efficient because it avoids reallocating the original slice and instead expands the capacity of the underlying array.
Similar to adding elements, we can use the built-in function append() to remove elements from a slice. In Go, there is no built-in deletion function, we need to use the append() function to delete elements in the slice. The following code demonstrates how to delete elements from a slice:
package main import "fmt" func main() { nums := []int{1, 2, 3, 4} fmt.Println(nums) // 输出 [1 2 3 4] nums = append(nums[:1], nums[2:]...) fmt.Println(nums) // 输出 [1 3 4] }
In the above code, we first initialize a slice containing 4 integers, and then use the append() function to delete the second element. Specifically, we first connect the subslice from the first element to the second element (exclusive) and the subslice from the third element to the last element, thereby obtaining a new slice and assigning it to Original slice.
It should be noted that since slice in Go is a structure, it contains a pointer to the underlying array, the length and capacity information of the slice, so when we delete an element in the slice, we must recreate it. A new slice and copies the elements from the original slice to the new slice.
If you want to modify multiple elements in the slice, you can use a for loop to traverse the slice and modify it in each iteration The current element. The following code demonstrates how to modify multiple elements in a slice:
package main import "fmt" func main() { nums := []int{1, 2, 3, 4} fmt.Println(nums) // 输出 [1 2 3 4] for i := 0; i < len(nums); i++ { nums[i] *= 2 } fmt.Println(nums) // 输出 [2 4 6 8] }
In the above code, we first initialize a slice containing 4 integers, then use a for loop to traverse the slice and multiply the current element by 2 .
It should be noted that when modifying multiple elements in a slice, the current element needs to be recalculated and copied to the slice during each iteration. Not only is this inefficient, it's also error-prone.
Summary
The above is an introduction to the modification operation of slice in Go. To modify individual elements in a slice, you can use indexing to access the elements in the slice just like an array. To add elements to a slice, we can use the built-in function append(). To remove an element from a slice, we can use the append() function and remove the specified element by concatenating two sub-slices. Finally, if you want to modify multiple elements in the slice, use a for loop to loop through the entire slice and modify the current element on each iteration.
The above is the detailed content of Discuss the modification operation of golang slice. For more information, please follow other related articles on the PHP Chinese website!