当前位置:网站首页>Slice slice
Slice slice
2022-07-28 04:07:00 【Violet and begonia】
One 、 Defining slices
Statement An array of unspecified sizes to define slices , Slicing doesn't need to indicate length
var identifier []type
Use make() Function to create slices
var slice1 []type = make([]type, len)
// Abbreviation
slice1 := make([]type, len)
// Specified capacity , among capacity Is an optional parameter
//len Is the length of the array and is also the initial length of the slice
make([]T, length, capacity)
Two 、 initialization
Initialize the slice directly
s := []int{
1, 2, 3}
Initialization slice s, It's an array arr References to
s := arr[:]
take arr From the subscript startIndex To endIndex-1 The element under is created as a new slice
s := arr[startIndex:endIndex]
Default endIndex It will mean until arr Last element of
s := arr[startIndex:]
Default startIndex From... To arr The first element of begins
s := arr[:endIndex]
By slicing s Initialization slice s1
s1 := s[startIndex:endIndex]
With built-in functions make() Initialization slice s,[]int Identify as its element type is int The section of
s := make([]int,len,cap)
3、 ... and 、len() and cap() function
- len(): To obtain the length of the
- cap(): Computing capacity
- A slice defaults to... Before initialization nil, The length is 0
Four 、 Intercept
Set the cut slice by setting the lower limit and upper limit [lower-bound:upper-bound]
number1 := numbers[:2]
5、 ... and 、 Additive elements append()
Append new element to slice
/* Add an element to the slice */
numbers = append(numbers, 1)
printSlice(numbers)
/* Add multiple elements at the same time */
numbers = append(numbers, 2,3,4)
printSlice(numbers)
6、 ... and 、 Copy slice elements to another slice copy()
Copy slice
/* Copy numbers Content to numbers1 */
copy(numbers1,numbers)
7、 ... and 、 Remove elements
seq := []string{
"a", "b", "c", "d", "e"}
index := 2
//... It means that you will seq[index+1:] Add entire to seq[:index] Back
seq = append(seq[:index], seq[index+1:]...)
边栏推荐
- Day08 redis的基础知识
- MySQL是怎么保证高可用的
- Kingbasees security guide of Jincang database -- 6.2. Configuration files related to authentication
- We must do these seven things well before leaving, and it's troublesome to do one less thing.
- How does MySQL ensure high availability
- xml文件使用及解析
- Leetcode 0140. word splitting II
- 常用的弱网测试工具
- Notes to subject 2
- 《关于我写自定义cell这件事》
猜你喜欢
![[day03] process control statement](/img/4d/d66140962b7e121a2fea2c366a972a.png)
[day03] process control statement

MATLB | location and constant volume IEEE30 node implementation of distributed energy

RT thread changes the print serial port (add other functions on the basis of BSP)

Dynamic planning - 62. Different paths

What is interface testing and its testing process

Filters, interceptors, listeners

Notes on writing questions in sword finger offer

21 days, fat brother personally takes you to play oauth2

Machine learning 07: Bayesian learning

Common weak network testing tools
随机推荐
巧用栈回溯,帮你快速定位问题
STC timer is abnormal (how to modify the initial value, the timing time is 100ms)
cookie与Session
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)
[untitled]
Common interface testing tools
40: Chapter 4: Development File Service: 1:fastdfs: (1): introduction to fastdfs;
Dynamic planning - 62. Different paths
"Three no's and five requirements" principle of enterprise Digitalization Construction
Notes to subject 2
[day03] process control statement
Ffmpeg common instructions
Do Netease and Baidu have their own tricks for seizing the beach AI learning machine?
【无标题】
Iterator function operation of iterator learning
测试用例管理工具
XML file usage and parsing
Kingbasees security guide of Jincang database -- 6.2. Configuration files related to authentication
Appnium -- app automated test tool
过滤器、拦截器、监听器