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

The concept and usage of go slices

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the concept and usage of go slices. In daily operation, I believe many people have doubts about the concept and usage of go slices. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the concept and usage of go slices. Next, please follow the editor to study!

Go slice prototype

/ / runtime/slice.gotype slice struct {the pointer to the underlying array len int// refers to the length of the array element. When the element is accessed using the subscript, the subscript cannot exceed the capacity of the len cap int// reference array, cap > = len}

Define a slice named baseSlice

BaseSlice: = [] int {0,1,2,3,4,5,6,7,8,9}

Underlying structure

Define a new slice slice1

Slice1: = baseSlice [2:5] / / [] int {2pm 3pm 4}

Underlying structure

Define a new slice slice2

Slice2: = slice1 [4:7] / / [] int {6, 7, 7, 8}

Underlying structure

Source code

Package mainimport ("fmt") func main () {baseSlice: = [] int {0,1,2 int 4, 5, 6, 7, 8, 9} slice1: = baseSlice [2:5] / / [] int {2 slice1 cap 4} / / here slice1 cap is 8, and the array referenced at the bottom is [] int {2 prides 4, 5, 6, 7, 8, 9} / so slice1 len=3 Cap=8 / / because of slice1 cap=8, slice1 [4:7] does not cause an array to cross the bounds slice2: = slice1 [4:7] / / [] int {6pc7je 8} fmt.Printf ("slice1:%v.len:%d.cap:%d\ n", slice1, len (slice1)) Cap (slice1)) / / slice1: [2 34] .len: 3.cap:8 fmt.Printf ("slice2:%v.len:%d.cap:%d\ n", slice2, len (slice2), cap (slice2)) / / slice2: [6 7 8] .len: 3.cap:4}

Slicing is value transfer

Package mainimport ("fmt") func main () {baseSlice: = [] int {0,1,2 slice1: = baseSlice [2:5] / / [] int {2 appendSlice (slice1) fmt.Printf ("slice1:%v\ n", slice1) / / slice1: [2 34] fmt.Printf ("baseSlice:%v\ n") BaseSlice) / / baseSlice: [0 1 2 3 4 10 6 7 8 9]} func appendSlice (s [] int) {/ / value passing s = append (s, 10) / / here changes the values in the underlying array If the number of elements in append is greater than the capacity of the underlying reference array, the array copy fmt.Printf ("append slice1:%v\ n", s) / / append slice1: [2 3 4 10]} will be triggered.

The illusion of reference transmission

If the value in the incoming slice len is modified in the called method, resulting in the corresponding value of the underlying array being modified, it looks as if the incoming slice value has been modified from the outside of the calling method.

Package mainimport ("fmt") func main () {baseSlice: = [] int {0,1,2 slice1 4, 5, 6, 7, 8, 9} slice1: = baseSlice [2:5] / / [] int {2 appendSlice (slice1) fmt.Printf ("slice1:%v\ n", slice1) / / slice1: [12334], it seems that slice1 has been modified In fact, the underlying array was modified because fmt.Printf ("baseSlice:%v\ n", baseSlice) / / baseSlice: [0 1123 3 4 5 6 7 8 9]} func appendSlice (s [] int) {/ / value passing / / s = append (s 10) s [0] = 123 / / here changes the value fmt.Printf ("append slice1:%v. Len=%d. Cap=%d\ n", s, len (s), cap (s)) / / append slice1: [12334] in the underlying array. Len=3. Cap=8}

Deep copy

Package mainimport ("fmt") func main () {baseSlice: = [] int {0,1,2,3,4,5,6 fmt.Printf ("slice1:%v\ n", slice1) / / slice1: [4 56] / / slice Deep copy slice2: = make ([] int, 3,3) copy (slice2) Slice1) fmt.Printf ("slice2:%v. Len=%d. Cap=%d\ n", slice2, len (slice2), cap (slice2)) / / slice2: [4 56]. Len=3. Cap=3 slice2 [0] = 100fmt.Printf ("slice2:%v\ n", slice2) / / slice2: [10056] fmt.Printf ("slice1:%v\ n", slice1) / / slice1: [4 56] fmt.Printf ("baseSlice:%v\ n", baseSlice) / / baseSlice: [0 1 2 3 4 5 6 7 8 9]}

Expand capacity

/ / runtime/slice.go//cap is the minimum capacity of func growslice (et * _ type, old slice, cap int) slice {. If cap > doublecap {newcap = cap} else {if old.len < 1024 {/ / if slice len=1024, increase the original capacity by 1/4 / / Check 0 < newcap to detect overflow / / and prevent an infinite loop. For 0 < newcap & & newcap < cap {newcap + = newcap / 4}...}}

Nil slice and empty slice

The empty slice underlying array pointer points to the address of the actual empty array

The underlying array pointer of nil slice points to nil.

Package mainimport ("fmt") func main () {var nilSlice [] int emptySlice: = make ([] int, 0) if emptySlice = = nil {fmt.Println ("emptySlice is nil.")} if nilSlice = = nil {fmt.Println ("nilSlice is nil.") / / output nilSlice is nil. }}

Tips:

The function argument of Go language is passed, only the value is passed.

At this point, the study on the concept and usage of go slices is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 228

*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