当前位置:网站首页>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 :
边栏推荐
- Use of MSF evaluation module
- Device = depthai Device(““, False) TypeError: _init_(): incompatible constructor arguments.
- ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
- Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
- Typescript -- preliminary study of variable declaration
- affair
- [TiO websocket] v. TiO websocket server counts the number of online people
- Openstack explanation (22) -- neutron plug-in configuration
- [software] ten skills to maximize the value of ERP system
- ES6新增特性--箭头函数
猜你喜欢

Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community
![报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s](/img/54/f42146ae649836fe7070ac90f2160e.png)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s

机器学习笔记 - 深度学习技巧备忘清单

Machine learning notes - spatial transformer network using tensorflow

Detailed explanation of the difference between construction method and method

Method (common method), method execution memory analysis, method overloading mechanism, method recursion

MSF adds back door to normal program

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

Simulation of map and set

Talk about how to customize data desensitization
随机推荐
Ecological co construction | 2021 streamnational excellent partner of the year comes out!
【方案开发】红外体温计测温仪方案
Flask (VIII) - form processing
OpenCV CEO教你用OAK(四):创建复杂的管道
Opencv oak-d-w wide angle camera test
Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
Some learning records I=
DOS command virtual environment
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
市场上的服装ERP体系到底是哪些类型?
Four data-driven behaviors of integral system
Day45 storage engine data type integer floating point character type date type enumeration and set type constraints table to table relationships
affair
Zhiyun health submitted the statement to HKEx again: the loss in 2021 exceeded 4billion yuan, an increase of 43% year-on-year
Use of MSF evaluation module
Package details
Detailed explanation of this and static
[share] how do enterprises carry out implementation planning?
[ERP system] how much do you know about the professional and technical evaluation?