Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the considerations for the use of Slice

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what are the matters needing attention in the use of Slice". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

For the use of slice in Go, 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 procedure is: the main function defines a 10-length integer array array, then defines a slice slice, cuts the sixth element of the array, and finally prints the length and capacity of the slice to determine whether the address of the first element of the slice is equal to that of the sixth element of the array.

If you think about whether the first element of the slice is equal to the sixth element of the array, maybe you can run this program to prove that you are looking down at the result. Well, I will not sell it. In the above program, slice is created according to the array array, and the storage space is shared with the array. The starting position of slice is array [5], length 1, capacity 5, slice [0] and array [5] address.

Next, let's take a look at this program. Try to run the program by 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 that the function AddElement () takes a slice and an element, append the element into the slice, and returns the slice. Define a slice in the main () function and append three elements to the slice, then call AddElement () to continue with the fourth element into the slice append and define a new slice newSlice. Finally, it is judged whether the new slice newSlice and the old slice slice share a piece of storage space.

It is believed that many partners in this program think that append may trigger the expansion of the old Slice, but they are not so sure. Then go on to see that "it is possible" to become "positive":

When the 1.append function executes, it will determine whether the capacity of the slice can store the new elements. If not, it will reapply for the storage space. The new storage space will be 2 times or 1.25 times the original storage space (depending on the size of the original space expansion). In this example, two append operations have actually been performed, and the space of the first time has increased to 4, so the second append will not be expanded, so the new and old slices will share the same storage space. The program outputs "true".

Then move on to see how this program will output, and you can think about 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, it would be better to look down with questions. The whole person has a feeling of sudden enlightenment. If you don't believe it, you can try it:

In the program, a slice with a length of 10 is defined as order,pollorder and lockorder, respectively, which are generated by order [start,stop,max] operation on order slices. Finally, the program prints the capacity and length of pollorder and lockorder respectively.

Order [start,stop,max] means to slice the order, the new slice range is [start,stop), and the new slice capacity is max. The orderLen,pollorder slice with twice the order length 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 both pollorder and lockerorder are orderLen, or 5.

This is the end of the content of "what are the points for attention in the use of Slice". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report