Home >Backend Development >Golang >golang slice addition and deletion
Golang slice is a very commonly used data structure. It is a dynamic array that supports automatic expansion and can easily operate and modify data in the program. The length of the slice can be changed dynamically, which provides a lot of flexibility to our code. Adding and deleting elements is a very common operation during the use of slice. This article will introduce how to add and delete elements in golang slice.
Let’s first review the basic operations of golang slice in order to better understand the process of adding and deleting elements. In golang, to define a slice, you need to use the make function. This function contains three parameters. The first one specifies the type of the slice, the second one specifies the length of the slice, and the third one specifies the capacity of the slice.
For example:
var s = make([]int, 3, 5)
The above code defines an int type slice with a length of 3 and a capacity of 5. The first parameter is the int type we defined, and the second The first parameter specifies the length of the slice to be 3, and the third parameter specifies the capacity of the slice to be 5. It should be noted that the capacity of a slice can be greater than the length, but the length cannot be greater than the capacity.
Next are some basic operations of golang slice:
1) Access the slice element
var s = []int {1, 2, 3, 4, 5} fmt.Println(s[0]) // 输出1
2) Modify the slice element
var s = []int {1, 2, 3, 4, 5} s[0] = 6 fmt.Println(s) // 输出[6 2 3 4 5]
3) Get the slice The length and capacity
var s = make([]int, 3, 5) fmt.Println(len(s)) // 输出3 fmt.Println(cap(s)) // 输出5
4) Slicing operation
var s = []int {1, 2, 3, 4, 5} fmt.Println(s[1:3]) // 输出[2 3]
In golang, slice element adding operation is Two ways are to use the append function and the " " operator.
Below, we will introduce the usage of these two methods respectively.
1) Use the append function to add elements
In golang, we can use the append function to dynamically add slice elements. Its syntax is as follows:
func append(s []T, vs ...T) []T
Among them, the first parameter s is a slice of type T, and the following parameter vs is a variable parameter list, also of type T, indicating the element to be added. The return value of this function is a new slice containing the added elements.
For example:
var s = []int {1, 2, 3, 4, 5} s = append(s, 6) fmt.Println(s) // 输出[1 2 3 4 5 6]
In the above code, we use the append function to add an element 6 to the slice, and then save the result back to the original slice.
If we want to add multiple elements to the slice, we only need to pass in these elements after the append function. For example:
var s = []int {1, 2, 3, 4, 5} s = append(s, 6, 7, 8) fmt.Println(s) // 输出[1 2 3 4 5 6 7 8]
It should be noted that if the capacity of the slice is insufficient, the append function will automatically expand its capacity, so its time complexity is O(1).
2) Use the " " operator to add elements
In addition to using the append function, you can also use the " " operator in golang to merge two slices. The operands of this operator are all slices, and the result is also a new slice.
For example, as shown below:
var s1 = []int {1, 2, 3} var s2 = []int {4, 5, 6} s := s1 + s2 fmt.Println(s) // 输出[1 2 3 4 5 6]
In this example, we add two slices and get a new slice s. It should be noted that the time complexity of the " " operator is O(n), because it requires opening a new array and copying the elements of the two slices into the new array.
If you want to delete an element in the golang slice, there are two methods, namely using the append function and using the copy function.
1) Use the append function to delete elements
We can use the append function's slicing operation to intercept the element to be deleted and the elements behind it, and then use the append function to recombine them. The specific implementation is as follows:
func Remove(slice []int, idx int) []int { return append(slice[:idx], slice[idx+1:]...) } func main() { var s = []int {1, 2, 3, 4, 5} s = Remove(s, 2) fmt.Println(s) // 输出[1 2 4 5] }
In this code, we use the Remove function to delete the third element in the slice. First, we combine the elements from slice0 to idx-1 and the elements from slice idx 1 to the end into a new slice. Then, we use the append function to save this new slice back to the original slice. Because the append function will automatically expand the capacity, there is no need to worry about insufficient capacity of the new slice.
It should be noted that the time complexity of this method is O(n), because it needs to copy n-1 elements to a new slice.
2) Use the copy function to delete elements
In addition to using the append function, we can also use the copy function to delete elements in the golang slice. The copy function can copy the elements in the src slice to the dst slice and return the number of copied elements.
The specific implementation is as follows:
func Remove(slice []int, idx int) []int { copy(slice[idx:], slice[idx+1:]) return slice[:len(slice)-1] } func main() { var s = []int {1, 2, 3, 4, 5} s = Remove(s, 2) fmt.Println(s) // 输出[1 2 4 5] }
In this code, we use the Remove function to delete the third element in the slice. Use the copy function to copy all elements after idx 1 to the idx position, and then reduce the length of the original slice by 1.
It should be noted that the time complexity of this method is also O(n), because it needs to copy n-1 elements to a new slice.
This article mainly introduces the operations of adding and deleting elements in golang slice. You can use the append function and " " operator to add elements, and you can use the append function and copy function to delete elements.
It is recommended to choose different methods according to the specific situation in actual programming. If you want to add or delete a small number of elements, it is more convenient to use the append function or the " " operator; if you want to add or delete a large number of elements, it is more efficient to use the copy function.
The above is the detailed content of golang slice addition and deletion. For more information, please follow other related articles on the PHP Chinese website!