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

Does modifying the value of a Go slice overwrite the value of the array?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, Xiaobian to share with you to modify the value of Go slices will cover the value of the array of relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Slicing and array array

An array is a sequence of numbered and fixed-length data items of the same unique type

Array declaration

Var identifier [len] type

Slice

Slice is a reference to a contiguous segment of an array, a slice is a reference type, and a slice is a pointer.

A slice is an array of variable length.

Slice declaration

Var identifier [] type

Slice initialization

Value modification of var slice1 [] type = arr [start:end] slices

Modify the value of the slice to override the value of the array

Code

Package mainimport "fmt" func main () {arr: = [5] int {1pjong 3je 4je 5} fmt.Printf ("slice modification before: array=%v len=%d cap=%d\ n", arr, len (arr), cap (arr)) s: = arr [0:3] fmt.Printf ("len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s) s = append (s) 6) fmt.Printf ("len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s) fmt.Printf ("slice modification: array=%v len=%d cap=%d\ n", arr, len (arr), cap (arr))}

Result

Slice modification before: array= [1 2 3 4 5] len=5 cap=5

Len=3 cap=5 ptr=0xc00000c300 slice= [1 2 3]

Len=5 cap=5 ptr=0xc00000c300 slice= [1 2 3 6 10]

Slice modification: array= [1 2 3 6 10] len=5 cap=5

Because the capacity of the underlying array is not exceeded, the address remains the same, and the array remains the same, so modifying the slice will overwrite the value of the array.

Modify the value that the slice does not overwrite the array

Code

Package mainimport "fmt" func main () {arr: = [5] int {1pjong 3je 4je 5} fmt.Printf ("slice modification before: array=%v len=%d cap=%d\ n", arr, len (arr), cap (arr)) s: = arr [0:3] fmt.Printf ("len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s) s = append (s) 6 fmt.Printf ("len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s) fmt.Printf ("slice modification: array=%v len=%d cap=%d\ n", arr, len (arr), cap (arr))}

Result

Slice modification before: array= [1 2 3 4 5] len=5 cap=5

Len=3 cap=5 ptr=0xc00000c300 slice= [1 2 3]

Len=6 cap=10 ptr=0xc0000141e0 slice= [1 2 3 6 10 11]

Slice modification: array= [1 2 3 4 5] len=5 cap=5

Beyond the capacity of the underlying array, the address changes, a new array is assigned, the returned slice points to the new array, and the value of the old array has not been modified.

Capacity expansion mechanism of slices

Slice decimal 1024

Code

Package mainimport "fmt" func main () {arr: = [5] int {1pje 2je 3je 4je 5} s: = arr [0:3] fmt.Printf ("len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s) s = append (s, 6d10) fmt.Printf ("len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s)}

Result

Before: len=3 cap=5 ptr=0xc00000c300 slice= [1 2 3]

After: len=6 cap=10 ptr=0xc0000141e0 slice= [1 2 3 6 10 11]

The capacity of the slice is 2 times that of the source slice.

Slice not less than 1024

Code

Package mainimport "fmt" func main () {arr: = [1024] int {1pje 2je 3Eng. Int 1024} s: = arr [0:] fmt.Printf ("before: len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s) s = append (s, 1025) fmt.Printf ("after: len=%d cap=%d ptr=%p slice=%v\ n", len (s), cap (s), s, s)}

Result

Before: len=1024 cap=1024 ptr=0xc000112000 slice= [12 3... 1024]

After: len=1025 cap=1280 ptr=0xc00012c000 slice= [12 3... 1024 1025]

The slice capacity has been increased by 1 stroke 4 to the capacity of the original slice.

Slice source code

If the capacity of the slice is not enough, growslice will be called to expand the capacity.

/ / go1.16.6 src/runtime/slice.gofunc growslice (et * _ type, old slice, cap int) slice {. / / code newcap: = newcap + newcap if cap > doublecap {newcap = cap} else {if old.cap < 1024 {newcap = doublecap} else {/ / Check 0 < newcap to detect overflow / / and prevent an infinite loop. For 0 < newcap & & newcap < cap {newcap + = newcap / 4} / / Set newcap to the requested cap when / / the newcap calculation overflowed. If newcap

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