当前位置:网站首页>Slice of go language foundation
Slice of go language foundation
2022-06-11 09:36:00 【Ink purple feather ink】
One 、 section :
1、 section : A slice is a reference to an array , So slices are reference types . But it's a structure in itself , Value copy passing . 2、 The length of the slice can be changed , therefore , Slicing is a variable array . 3、 Slice traversal is the same as array traversal , It can be used len() Find the length . Represents the number of available elements , Read and write operations cannot exceed this limit . 4、cap We can work out slice Maximum expansion capacity , Cannot exceed array limit .0 <= len(slice) <= len(array), among array yes slice Array of references . 5、 Definition of slice :var Variable name [] type , such as var str []string var arr []int. 6、 If slice == nil, that len、cap The result is equal to 0.
1、 Defining slices
package main
import "fmt"
func test01() {
//1. Declaration slice
var s1 []int
fmt.Println("s1:", s1)
if s1 == nil {
fmt.Println("s1 It's empty ")
} else {
fmt.Println("s1 Not empty ")
}
// 2.:=
s2 := []int{}
fmt.Println("s2:", s2)
// 3.make()
var s3 []int = make([]int, 5)
fmt.Println("s3:", s3)
// 4. Initialize assignment
var s4 []int = make([]int, 0, 0)
fmt.Println("s4:", s4)
s5 := []int{1, 2, 3}
fmt.Println("s5:", s5)
// 5. Slice from array
arr := [5]int{1, 2, 3, 4, 5}
var s6 []int
// Front bag, back bag
s6 = arr[1:4]
fmt.Println("s6:", s6)
}
func main() {
test01()
}Running results :
2、 Slice initialization
func test02() {
arr := [...]int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
slice1 := arr[2:8]
slice2 := arr[:6]
slice3 := arr[5:]
slice4 := arr[:]
slice5 := arr[:len(arr)-1] // Remove the last element of the slice
fmt.Printf("arr %v\n", arr)
fmt.Printf("slice1: %v\n", slice1)
fmt.Printf("slice2: %v\n", slice2)
fmt.Printf("slice3: %v\n", slice3)
fmt.Printf("slice4: %v\n", slice4)
fmt.Printf("slice5: %v\n", slice5)
}Running results :
3、 Modification of slices :
func test03() {
darr := [...]int{57, 89, 90, 82, 100, 78, 67, 69, 59}
dslice := darr[2:5]
fmt.Println("array before", darr)
for i := range dslice {
dslice[i]++
}
fmt.Println("array after", darr)
}Running results :
4、append Method :
func test04() {
slice1 := []int{1, 2, 3}
fmt.Println("slice1:", slice1)
slice2 := append(slice1, 4, 5, 6)
fmt.Println("slice2:", slice2)
slice3 := []int{7, 8, 9}
slice4 := append(slice1, slice3...)
fmt.Println("slice4:", slice4)
}Running results :
5、len() and cap()
func test05() {
slice1 := []string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
slice2 := slice1[1:3]
fmt.Printf("len: %d cap: %d", len(slice2), cap(slice2))
}Running results :
6、copy()
func test06() {
s1 := []int{1, 2, 3, 4, 5}
s2 := make([]int, 10)
fmt.Printf("before s1 : %v\ns2 : %v\n", s1, s2)
copy(s2, s1)
fmt.Printf("after s1 : %v\ns2 : %v\n", s1, s2)
}Running results :
7、 Traverse
func test07() {
slice1 := []string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
for id, v := range slice1 {
fmt.Printf("id:%v, value:%v\n", id, v)
}
}Running results :
边栏推荐
- When the enterprise makes a decision, which part should lead the ERP project?
- Comment l'entreprise planifie - t - elle la mise en oeuvre?
- Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
- Some learning records I=
- Tissu. JS définit dynamiquement la taille de la police
- 报错device = depthai.Device(““, False) TypeError: _init_(): incompatible constructor arguments.
- Don't use redis list to implement message queue. Stream is designed for queues
- [intelligent development] scheme design and hardware development of sphygmomanometer
- Flask (II) - route
- Automation operation and maintenance articles collection
猜你喜欢

Redis source code analysis hash object (z\u hash)

MSF adds back door to normal program

Comparison and introduction of OpenCV oak cameras

报错ModularNotFoundError: No module named ‘find_version’

Opencv oak-d-w wide angle camera test

Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods

Don't use redis list to implement message queue. Stream is designed for queues

Use of MSF evaluation module

Bowen dry goods | Apache inlong uses Apache pulsar to create data warehousing

Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
随机推荐
面试题 17.10. 主要元素
The mobile terminal page uses REM for adaptation
Where is it safer to open an account for soda ash futures? How much does it cost to buy at least one hand?
Opencv oak-d-w wide angle camera test
What problems can ERP system help enterprises deal with?
Sword finger offer II 041 Average value of sliding window
报错device = depthai.Device(““, False) TypeError: _init_(): incompatible constructor arguments.
When the enterprise makes a decision, which part should lead the ERP project?
Customize PCBA scheme and develop wrist sphygmomanometer scheme
What are the types of garment ERP system in the market?
报错ModularNotFoundError: No module named ‘find_version’
机器学习笔记 - 深度学习技巧备忘清单
Importance of implementation of clothing ERP in the project
[software] ten skills to maximize the value of ERP system
[scheme design] scheme of household oximeter based on single chip microcomputer
A summary of the problem type and method for proving the limit of sequence in postgraduate entrance examination
OpenCV OAK相机对比及介绍
The difference and relation between machine learning and statistics
远程办公最佳实践及策略
Do you know these advantages of ERP system?