In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to realize slice slicing structure in Go language". In daily operation, I believe many people have doubts about how to realize slice slicing structure in GE language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize slice slicing structure in Go language". Next, please follow the editor to study!
Convert an array to a slice
The copy code is as follows:
A: [10] int {}
Fmt.Println (a)
S1: = a [: 10] / / take the first 10 elements [5:] take 5-the last element
Fmt.Println (S1)
Slice test
The copy code is as follows:
A: = [] byte {'a', 'baked,' crested, 'dashed,' estranged, 'faded,' h'}
Sa: = a [2:5]
Fmt.Println (string (sa))
Sd1: = a [3:5]
Fmt.Println (string (sd1)) / / look at the effect
What we see here is that slice_a points to Array_ori, and it actually points from c to k. We use fmt.Println (cap (slice_a)) and the result is definitely not 3.
Try this on your own.
The copy code is as follows:
A: = [] byte {'a', 'baked,' crested, 'dashed,' estranged, 'faded,' h'}
Sa: = a [2:5]
Fmt.Println (string (sa))
S: = sa [1:3]
Fmt.Println (string (s)
S2: = sa [3:5]
Fmt.Println (string (S2))
Slice points to the underlying array. If multiple slice points to the same array, one of them changes, and all others change. Try this one below.
The copy code is as follows:
A: = [] int {1,2,3,4,5}
S1: = a [2:5]
S2: = a [1:3]
Fmt.Println (S1, S2)
S1 [0] = 9
Fmt.Println (S1, S2)
Slices are reference types, that is, if you assign slices to another slice, they all point to the same underlying array. For example, if a function takes a slicing parameter, changes to its elements appear in the caller, similar to passing a pointer to an underlying array. Therefore, the Read function can accept slice parameters without pointers and counts; the length of the slice determines the upper limit of readable data. Here is the signature of the File-type Read method of the os package:
The copy code is as follows:
Func (file * File) Read (buf [] byte) (n int, err os.Error)
This method returns the number of bytes read and possible error values. To read the first 32 bytes of a large buffer b, slice (verb) buffer.
The copy code is as follows:
N, err: = f.Read (buf [0:32])
This kind of slicing is common and efficient. In fact, regardless of efficiency, this fragment can also read the first 32 bytes of the buffer.
The copy code is as follows:
Var n int
Var err os.Error
For I: = 0; I
< 32; i++ { nbytes, e := f.Read(buf[i:i+1]) // Read one byte. if nbytes == 0 || e != nil { err = e break } n += nbytes } 只要还在底层数组的限制内,切片的长度可以改变,只需赋值自己。切片的容量,可用内部函数 cap 取得,给出此切片可用的最大长度。下面的函数给切片添值。如果数据超过容量,切片重新分配,返回结果切片。此函数利用了 len 和 cap 对 nil 切片合法、返回0的事实。 Apppend的用法 复制代码 代码如下: a := make([]int, 3, 6) fmt.Printf("%p", a) a = append(a, 1, 2, 3) fmt.Printf("%v %p\n", a, a) a = append(a, 1, 2, 3) fmt.Printf("%v %p\n", a, a) 我们必须返回切片,因为尽管 Append 可以改变 slice 的元素, 切片自身(持有指针、长度和容量的运行态数据结构)是值传递的。添加切片的主意很有用,因此由内置函数 append 实现。 复制代码 代码如下: func Append(slice, data[]byte) []byte { l := len(slice) if l + len(data) >Cap (slice) {/ / reallocate
/ / Allocate double what's needed, for future growth.
NewSlice: = make ([] byte, (l+len (data)) * 2)
/ / Copy data (could use bytes.Copy ())
For I, c: = range slice {
NewSlice [I] = c
}
Slice = newSlice
}
Slice = slice [0: l+len (data)]
For I, c: = range data {
Slice [lumbi] = c
}
Return slice
}
When the element appended by append in slice exceeds the pointing capacity, it will repoint to a new underlying array, so a change in one underlying array will not lead to other changes, so try the following code
The copy code is as follows:
A: = [] int {1,2,3,4,5}
S1: = a [2:5]
S2: = a [1:3]
Fmt.Println (S1, S2)
S2 = append (S2, 1, 2, 2, 3, 3, 4, 5)
S1 [0] = 9
Fmt.Println (S1, S2)
Copy
This is a copied function, and the following code is copied from S2 to S1, and then we will see that the result is [7 8 9 4 5].
If it's copy (s 2), the result we see is [1 2 3].
The copy code is as follows:
S1: = [] int {1, 2, 3, 4, 5}
S2: = [] int {7, 8, 9}
Copy (S1, S2)
Fmt.Println (S1)
At this point, the study on "how to implement slice slicing structure in Go language" 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: 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.