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 is the reason why the Go language Slice slicing operation will confuse the original data?

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what is the reason why the Go language Slice slicing operation will confuse the original data?" in the actual case operation process, many people will encounter such a dilemma, and then 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!

In the process of learning the Go language, we all know that slicing can be done through slicing operation.

For example

Sl: = make ([] int) 10) a = sl [1:] / means to take the element starting with subscript 1 to the end b = sl [: 5] / / to take the element with subscript from 0 to the element with subscript 4 c = sl [1:5] / / to take the element with subscript from 1 to the element with subscript 4 d = sl [1:5:6] / / to take the element with subscript from 1 to element with subscript 4 And set the capacity of the new slice to 6 * Note, this operation is critical to make a deep copy of the original slice.

The slices obtained when the slice is obtained through [mvig n] are all based on the underlying array of the original slice, that is, when you have some data in the new slice, the data of the original slice will also be changed, which is not usually what we want. Of course, if the expansion does not occur when using append () to operate on the new slice, then the value of the original slice will also change, so It is recommended to use [m:n:s] as much as possible when performing the slice acquisition operation. This is usually to open up a new space to copy the data of the original slice. Of course, if the slicing operation is to change the original slice, try to use [MRAN], which will save the space opened up by memory.

Code demonstration:

A: = [10] int {1,2} I: = a [: 6] y: = [] int {3prit 4} fmt.Printf ("a->% drect a->% v\ n", cap (a), a) fmt.Printf ("I->% drect I->% v\ n", cap (I), I) fmt.Printf ("y->% drect y->% v\ n", cap (y) Y) fmt.Println ("= =") / / I [0] = 100 / / fmt.Printf ("a->% d _ r _ r a->% v\ n", cap (a), a) / / fmt.Printf ("I->% d _ line I->% v\ n", cap (I), I) / / fmt.Printf ("y->% d _ r y->% v\ n", cap (y)) Y) / / fmt.Println ("=") I = append (I, y...) Fmt.Printf ("a->% drecinct a->% v\ n", cap (a), a) fmt.Printf ("I->% drect I->% v\ n", cap (I), I) fmt.Printf ("y->% drect y->% v\ n", cap (y), y)

Results:

This is the end of the introduction of "what is the reason why Go Slice slicing operations confuse the original data?" 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

Internet Technology

Wechat

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

12
Report