Home  >  Article  >  Backend Development  >  Share notes on using Slice in go

Share notes on using Slice in go

藏色散人
藏色散人forward
2021-10-13 17:28:522285browse

This article is provided by the go language tutorial column to share with you the precautions for using Slice in go. I hope it will be helpful to friends in need!

This article discusses the use of slice in Go. Let’s take a look at the following program.

package mainimport (
    "fmt"
    )func main() {
    var array [10]int
    var slice = array[5:6]
    fmt.Println("lenth of slice: ", len(slice))
    fmt.Println("capacity of slice: ", cap(slice))
    fmt.Println(&slice[0] == &array[5])}
  • What I want to say about this program is: defined in the main function Create a 10-length integer array array, then define a slice, cut the 6th element of the array, and finally print the length and capacity of the slice to determine whether the address of the first element of the slice and the 6th element of the array are equal.
  • Everyone think about whether the first element of the slice is equal to the 6th element of the array. Maybe you can run this program to prove it and then look at the results below. Okay, I won’t be trivial. The above program The slice is created with the data array array and shares storage space with the array. The starting position of the slice is array[5], the length is 1, and the capacity is 5. The addresses of slice[0] and array[5] are the same.

Next, let’s take a look at this program and try to run it yourself. Hands-on practice is the best teacher

package mainimport (
    "fmt")func AddElement(slice []int, e int) []int {
    return append(slice, e)
    }func main() {
    var slice []int
    slice = append(slice, 1, 2, 3)
    newSlice := AddElement(slice, 4)
    fmt.Println(&slice[0] == &newSlice[0])}
  • What I want to express in the above paragraph is The function AddElement() accepts a slice and an element, appends the element into the slice, and returns the slice. Define a slice in the main() function and append 3 elements to the slice, then call AddElement() to continue appending the fourth element to the slice and define a new slice newSlice. Finally, it is judged whether the new slice newSlice and the old slice share a storage space
  • This program believes that many friends think that append may trigger the expansion of the old Slice, but they are not sure about it. Let’s read on. "Possible" has become "definitely":
    1. When the append function is executed, it will determine whether the slice capacity can store the new elements. If not, it will re-apply for storage space, and the new storage space will be twice the original size. Or 1.25 times (depending on the size of the original expanded space). In this example, two append operations are actually performed. The first space increases to 4, so the second append will not expand again, so the old and new slices will share a piece of storage. space. The program will output "true".

Then continue to see how this program will output. You can think about it or run the program:

 package mainimport (
    "fmt")func main() {
    orderLen := 5
    order := make([]uint16, 2 * orderLen)
    pollorder := order[:orderLen:orderLen]
    lockorder := order[orderLen:][:orderLen:orderLen]
    fmt.Println("len(pollorder) = ", len(pollorder))
    fmt.Println("cap(pollorder) = ", cap(pollorder))
    fmt.Println("len(lockorder) = ", len(lockorder))
    fmt.Println("cap(lockorder) = ", cap(lockorder))}

After running the above program, you will have questions and continue to read below. Better, the whole person has a feeling of enlightenment. If you don’t believe it, you can try:

  1. The program defines a slice order with a length of 10, pollorder and lockorder respectively order the order slice [start ,stop,max] operation generates the slice, and finally the program prints the capacity and length of pollorder and lockorder respectively.
  2. order[start,stop,max] means to slice the order, the new slice range is [start, stop), and the new slice capacity is max. The order length is 2 times the orderLen. The pollorder slice refers to the first half of the order, and the lockorder refers to the second half of the order, that is, the original order is divided into two segments. Therefore, the length and capacity of pollorder and lockerorder are both orderLen, which is 5.

This article explains some of the use of Slice. I hope it can help partners in need. For more information about the use of Slice, please leave a message to discuss

The above is the detailed content of Share notes on using Slice in go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete