In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to add, delete, modify and query slices in Go language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to slice, add, delete, modify and check" in Go language.
Introduction
Golang's array is a fixed-length array that can hold a collection of elements of the same data type.
But when the length is fixed, there must be some limitations when using it, for example: the length of the application is too long to waste memory, too small and not enough.
In view of the above reasons, we have slicing in go language, which can be understood as an array of variable length, which is actually implemented in an array at the bottom, adding automatic expansion function.
A Slice is a variable-length sequence with elements of the same type.
First, the basic grammar of slicing 1. Grammar
Declaring a slice is similar to declaring an array, as long as no length is added.
Var identifier [] type
Slices are reference types, and you can use the make function to create slices:
Var slice1 [] type = make ([] type, len)
It can also be abbreviated as
Slice1: = make ([] type, len)
You can also specify the capacity, where capacity is an optional parameter
Make ([] Themagement capacity)
Where len is the length of the array and also the initial length of the slice
two。 Example
The slice is null before it is assigned
Create slices by declaring variables
Package main import "fmt" func main () {var names [] string var numbers [] int fmt.Printf ("names:% v\ n", names) fmt.Printf ("numbers:% v\ n", numbers) fmt.Println (names = = nil) fmt.Println (numbers = = nil)}
The output is as follows
Names: []
Numbers: []
True
True
Create a slice using the make function
Package main import "fmt" func main () {var a = make ([] int, 2) var b = make ([] int, 3) fmt.Printf ("a:% v\ n", a) fmt.Printf ("b:% v\ n", b)}
The output is as follows
A: [0 0]
B: [0 0 0]
3. Length and capacity of slices
Slices have their own length and capacity, and we can calculate the length by using the built-in len () function and the contents of the slice by using the built-in cap () function.
Package main import "fmt" func main () {var names = [] string {"hello", "world"} var num = [] int {1,2,3} fmt.Printf ("len:% d cap:% d\ n", len (names), cap (names)) fmt.Printf ("len:% d cap:% d\ n", len (num), cap (num)) fmt.Printf ("num [2]:% v\ n" Num [2]) / / get the slice elements fmt.Println ("-") var a = make ([] string, 2,3) fmt.Printf ("len:% d cap:% d", len (a), cap (a))} according to the subscript index
The output is as follows
Len: 2 cap: 2
Len: 3 cap: 3
Num [2]: 3
-
Len: 2 cap: 3
2. Initialization of slices
There are many ways to initialize slices, either directly or using an array.
1. Directly initialize package main import "fmt" func main () {a: = [] int {1,2,3} fmt.Printf ("a:% v\ n", a)}
The output is as follows
A: [1 2 3]
two。 Initialize package main import "fmt" func main () {a: = [...] int {1,2,3} b: = a [:] fmt.Printf ("b:% v\ n", b)} with an array
Output result
B: [1 2 3]
3. Class with some elements of the array (slice expression)
At the bottom of the slice is an array, so we can get the slice through the slice expression based on the array.
The low and high in the slice expression represent an index range (the left side of the packet does not cover the right side), resulting in the length of the slice = high-low, and the capacity is equal to the capacity of the underlying array of the resulting slice.
Package main import "fmt" func main () {a: = [...] int {1, 2, 3, 4, 5, 6, 7, 8} b: = a [2:5] / / subscript 2 to 5, close left and open right Excluding 5 fmt.Printf ("b:% v\ n", b) c: = a [2:] / / all fmt.Printf ("c:% v\ n", c) d: = a [: 3] / / before subscript 3 Does not include 3 fmt.Printf ("d:% v\ n", d) e: = a [:] / / take all values fmt.Printf ("e:% v\ n", e)}
The output is as follows
B: [3 4 5]
C: [3 4 5 6 7 8]
D: [1 2 3]
E: [1 2 3 4 5 6 7 8]
4. Empty (nil) slice
Before initialization, a slice defaults to nil, length 0 and capacity 0
Package main import "fmt" func main () {var a [] int fmt.Println (a = = nil) fmt.Printf ("len:% d\ n", len (a), cap (a))}
The output is as follows
True
Len: 0,cap: 0
Third, the traversal of slices
Traversal of slices and arrays is very typed and can be traversed using for circular indexes or for range loops
1. For loop traverses package main import "fmt" func main () {S1: = [] int {1, 2, 3, 4, 5, 6} for I: = 0; I < len (S1); iTunes + {fmt.Printf ("S1 [% d]:% v\ n", I, S1 [I])}}
The output is as follows
S1 [0]: 1
S1 [1]: 2
S1 [2]: 3
S1 [3]: 4
S1 [4]: 5
S1 [5]: 6
2. For range traverses package main import "fmt" func main () {S1: = [] int {1, 2, 3, 4, 5, 6} for I, v: = range S1 {fmt.Printf ("I:% v\ n", I, v)}}
The output is as follows. I is the index and v is the value.
I: 0,v: 1
I: 1,v: 2
I: 2,v: 3
I: 3,v: 4
I: 4,v: 5
I: 5,v: 6
Fourth, the addition and deletion of slice elements copy
A slice is a dynamic array, and you can add elements using the append () function
There is no special way to delete slice elements in the go language, we can use the features of the slice itself to delete elements.
Because the slice is a reference type, the original content will be modified by assignment. Go provides the copy () function to copy the slice.
1. Add element package main import "fmt" func main () {a: = [] int {} a = append (a, 1) a = append (a, 2) a = append (a, 3, 4, 5) / / add multiple elements fmt.Printf ("a:% v\ n", a) fmt.Println ("-") A1: = [] int {3,4 5} a2: = [] int {1,2} a2 = append (a2, A1...) / / add another slice into the fmt.Printf ("a2:% v\ n", a2)}
The output is as follows
A: [1 2 3 4 5]
-
A2: [1 2 3 4 5]
two。 Delete element
Package main import "fmt" func main () {var S1 = [] int {1,2,3,4} fmt.Println ("--before deletion -") fmt.Printf ("S1:% v\ n", S1) / / delete 3, whose subscript index is 2 S1 = append (S1 [: 2], S1 [3:]...) Fmt.Println ("--after deletion--") fmt.Printf ("S1:% v\ n", S1)}
The execution result is as follows
-before deletion-
S1: [1 2 3 4]
-after deletion-
S1: [1 2 4]
A formula for deleting an element
To delete an element with index index from slice a, do the following
A = append (a [: index], a [index + 1:]) 3. Modify the value of slice element package main import "fmt" func main () {var S1 = [] int {1,2,3,4,5} S1 [1] = 100 / / Index 1 to 100 fmt.Printf ("S1:% v\ n", S1)}
The output is as follows
S1: [1 100 3 4 5]
4. Find the slice element package main import "fmt" func main () {var S1 = [] int {1,2,3,4,5} var key = 2 / / find the location for I, v: = range S1 {if v = = key {fmt.Printf ("S1:% v\ n", S1) fmt.Printf ("index is:% v\ n", I)}
The output is as follows
S1: [1 2 3 4 5]
The index is: 1
5. Copy slice package main import "fmt" func main () {var S1 = [] int {1,2,3,4,5} var S2 = S1 S2 [0] = 100fmt.Printf ("S1:% v\ n", S1) fmt.Printf ("S2:% v\ n", S2)}
The output is as follows
S1: [100 2 3 4 5]
S2: [100 2 3 4 5]
You can see that when the value of S2 changes, the value of S1 also changes, because S2 copies S1 is his memory address, so the association will change.
Modification using the copy method does not affect the value of the source slice
Package main import "fmt" func main () {var S1 = [] int {1, 2, 3, 4, 5} var S2 = make ([] int, 4) / / the type that needs to make a slice, specify 5 elements copy (S2, S1) / / specify copied slice S2 [0] = 100fmt.Printf ("S1:% v\ n", S1) fmt.Printf ("S2:% v\ n", S2)}
Output result
S1: [1 2 3 4 5]
S2: [100 2 3 4 5]
At this point, I believe that everyone on the "Go language how to achieve slicing add, delete, change and query" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.