当前位置:网站首页>[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
2022-07-30 09:11:00 【Yugong move code】
文章目录
一、切片操作
1.切片
切片和数组类似,are data sets.和数组不同的是,A slice is a piece of contiguous space of dynamically allocated size.
2.切片的使用
2.1 切片的格式
var 变量名 []T //T 表示切片类型.
相关案例:
package main
import "fmt"
func main() {
// 声明整型切片
var numList []int
// 声明字符串切片
var strList []string
// 声明一个空切片, {} Indicates that memory has been allocated,But the elements inside the slice are empty
var numListEmpty = []int{
}
// 输出3个切片
fmt.Println(numList, strList, numListEmpty)
// 输出3个切片大小
fmt.Println(len(numList), len(strList), len(numListEmpty))
// Slicing determines whether the result is empty
fmt.Println(numList == nil)
fmt.Println(strList == nil)
fmt.Println(numListEmpty == nil)
}

2.2 make() Function definition element
//T : The type of elements in the slice;
//size : Indicates how many elements are allocated for this type;
//cap : 预分配的元素数量,After this value is set, it has no effect size, Represents space allocated in advance,Setting it is mainly used to reduce dynamic expansion,造成的性能问题.
make( []T, size, cap )
相关案例:
package main
import "fmt"
func main() {
a := make([]int, 10)
b := make([]int, 10, 20)
fmt.Println(a, b)
fmt.Println(len(a), len(b))
}

3.append() The function adds elements
Go 语言中的内置函数 append() 可以为切片动态添加元素, 案例如下:
package main
import "fmt"
func main() {
// Declare a slice of type string
var strList []string
// Cyclic dynamic direction strList 切片中添加 20 个元素,and print the relevant parameters
for i := 0; i < 10; i++ {
line := fmt.Sprintf("愚公 %d", i)
strList = append(strList, line)
fmt.Printf("len: %d, cap: %d, pointer: %p, content: %s\n", len(strList), cap(strList), strList, strList[i])
}
// 添加切片
list := []string{
"愚公 10", "愚公 11"}
// list 后面的 ... 表示将 list 整个添加到 strList 切片中
strList = append(strList, list...)
fmt.Println(strList)
}

4.切片截取
slice [开始位置:结束位置]
- slice 表示切片.
- The start and end positions correspond to the subscripts of the target slice.
相关案例:
package main
import "fmt"
func main() {
// 添加切片
list := []string{
"愚公 10", "愚公 11", "愚公 12"}
// list 后面的 ... 表示将 list 整个添加到 strList 切片中
fmt.Println(list, list[1:2])
}

package main
import "fmt"
func main() {
// 添加切片
list := []string{
"愚公 10", "愚公 11", "愚公 12"}
// list 后面的 ... 表示将 list 整个添加到 strList 切片中
fmt.Println(list, list[1:2], list[1:], list[:2], list[:])
}

- If you do not fill in the end position,如 list[1:], It means from subscript 1 to the end of the array.
- If you do not fill in the starting position,如 list[:2],则表示从 0 到下标 2的位置.
- If the start position and end position are not filled in,如 list[:], will generate a slice identical to the original slice.
5.切片复制
copy( 原切片, 目标切片 []T) int
相关案例:
package main
import "fmt"
func main() {
// 设置元素数量为 10
const count = 10
// 源分片
list1 := make([]int, count)
// Assign a value to the source shard
for i := 0; i < count; i++ {
list1[i] = i
}
// target shard
list2 := make([]int, count)
// 将 srcSlice The shard's data is copied to destSlice 中
copy(list2, list1)
fmt.Println(list1)
fmt.Println(list2)
}

6.切片删除
Go The language does not provide a specific function to remove elements from a slice,可以利用appendTo achieve slice splicing to delete.
package main
import "fmt"
func main() {
// Declare a slice of type string
arr := []string{
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
// Print the elements before and after the deletion position, arr[:index] Represents the previous part of the data of the removed element,arr[index+1:] Represents the data behind the removed element
fmt.Println(arr[:1], arr[2:])
// Concatenate the elements before and after the deletion point
arr = append(arr[:1], arr[2:]...)
fmt.Println(arr)
}

边栏推荐
- Field interpretation under "Surgical variables (RX SUMM-SURG OTH REG/DIS)" in SEER database
- 【蓝桥杯选拔赛真题45】Scratch猫鼠游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
- Farthest Point Sampling - D-FPS vs F-FPS
- ACL 2022 | 引入角度margin构建对比学习目标,增强文本语义判别能力
- 电源完整性基础知识
- Golang DES 加解密如何实现?
- hcip 第14天学习笔记
- 阿里云国际版云服务器防火墙设置
- [Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail
- input标签的tabindex属性 & a标签的tabindex属性
猜你喜欢

C语言经典练习题(3)——“汉诺塔(Hanoi)“

jdbc ResultSetMetaData获取tableName问题

EMC过不了?都是PCB工程师的锅?

typescript5 - compile and install ts code

Detailed explanation of 4D words: C language three-point chess advanced + N-piece chess recursive dynamic judgment of winning or losing

2022年施工企业数字化转型思考,施工企业数字化转型之路

typescript7-typescript common types

sql注入数据库原理详解

剖析SGI STL空间配置器(allocate内存分配函数)

智能存储柜——解决您的存储需求
随机推荐
【SQL server速成之路】——身份验证及建立和管理用户账户
Detailed explanation of 4D words: C language three-point chess advanced + N-piece chess recursive dynamic judgment of winning or losing
【三子棋】——玩家VS电脑(C语言实现)
如何使用 Jmeter 进行抢购、秒杀等场景下,进行高并发?
香港服务器iis配置web服务器如何操作?
[Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail
hcip第八天
typescript4-安装编译ts的工具包
typescript2-typescript为什么给js添加类型支持
用示波器揭示以太网传输机制
桌面软件开发框架大赏
leetcode力扣——一篇文章解决多数之和问题
负电压电路(原理分析)
[GAN]老照片修复Bringing Old Photos Back to Life论文总结
MagicDraw二次开发过程
npm指令
js currying
电源完整性的去耦和层间耦合电容
【sleuth + zipkin 服务链路追踪】
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs