当前位置:网站首页>[Go Affair] See through Go's collections and slices at a glance
[Go Affair] See through Go's collections and slices at a glance
2022-07-31 10:42:00 【Dream of the eraser】
️ Go 集合
在 Go The keywords associated with collections in are Map,It represents an unnecessary collection of key-value pairs,Map 可以通过 Key 检索值,这一点和 Python The dictionary in is especially similar.
集合是无序的,并且使用 hash 表实现.
定义集合
使用 map Keywords define collections,原型如下:
var map_name map[key_type]value_type
参数说明如下:
map_name
:集合变量名;key_type
:键类型;value_type
:值类型.
Go 中的 map 可动态扩展,There is no need to declare the length like an array.
Next, make a simple declaration and use,代码如下:
package main
import (
"fmt"
)
func main() {
// 创建集合
var map_var map[string]string
// 在使用 map 前,需要先 make,Its role is to give map 分配数据空间
map_var = make(map[string]string)
map_var["red"] = "红色"
map_var["green"] = "绿色"
map_var["blue"] = "蓝色"
for color := range map_var {
fmt.Println(color, "中文:", map_var[color])
}
}
Be careful if you create a collection,但是没有使用 make
函数,就会报错,The reason is also unallocated memory.
Do not use if desired map
You can declare and initialize collections,可以在声明的时候,直接赋值.
package main
import (
"fmt"
)
func main() {
// 创建集合
map_var := map[string]string{
"red": "红色",
"green": "绿色",
"blue": "蓝色",
}
for color := range map_var {
fmt.Println(color, "中文:", map_var[color])
}
}
Map 元素删除
直接使用内置的 delete
方法即可.
package main
import (
"fmt"
)
func main() {
// 创建集合
map_var := map[string]string{
"red": "红色",
"green": "绿色",
"blue": "蓝色",
}
// 删除一个元素
delete(map_var, "red")
for color := range map_var {
fmt.Println(color, "中文:", map_var[color])
}
}
Go 切片
在 Python Mentioned in slicing is only taking a subset of the list,但是在 Go is an abstraction of an array,在 Go Medium slices are one type,Also called dynamic array,Expansion elements can be added.
A slice structure consists of three parts,Pointer
指向数组的指针,len
切片的长度,cap
切片容量,容量大于等于长度.
We can declare directly【size not set】An array of defined slices,The basic syntax format is :
var identifier []type
The above statement defaults to nil
,其中 len=0
,cap=0
.
Use the following code to test.
package main
import "fmt"
func main() {
var slice_var []int
sliceFormat(slice_var)
if slice_var == nil {
fmt.Printf("空切片")
}
}
func sliceFormat(x []int) {
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}
Declare a slice with an initial length or a specific capacity,使用 make
函数完成.
var slice_var []type = make([]type, length, capacity)
where the first parameter is the type,第二个是切片的长度,在这种情况下,So is the capacity of the slice 5,The following code can be tested.
capacity
可以缺省,即下述代码:
package main
import "fmt"
func main() {
num_slice := make([]int, 5)
printSlice(num_slice)
}
func printSlice(x []int) {
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}
运行代码,得到下述输出:
len=5 cap=5 slice=[0 0 0 0 0]
You can test switching different types,例如 bool
,Slices are automatically initialized by the system as false
.
len=5 cap=5 slice=[false false false false false]
Initialize the slice while declaring it
package main
import "fmt"
func main() {
slice_var := []int{
1, 2, 3}
printSlice(slice_var)
}
func printSlice(x []int) {
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}
The above code directly completes the slice declaration and initialization operations,Indicates slice usage []
符号,{1,2,3}
Indicates initialization data.
Elements can be added to already declared slices,需要使用内建函数 append
添加元素.
package main
import "fmt"
func main() {
num_slice := make([]int, 5, 10)
num_slice = append(num_slice, 6)
printSlice(num_slice)
}
func printSlice(x []int) {
fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}
After multiple verifications,will be found when the length is greater than 2 时,The capacity of the slice will be automatically expanded to the original one 2 倍.
# 当 len =10 ,cap =10 时,输出结果
len=10 cap=10 slice=[0 0 0 0 0 6 7 8 9 10]
# The result after expansion
len=11 cap=20 slice=[0 0 0 0 0 6 7 8 9 10 11]
你正在阅读 【梦想橡皮擦】 的博客
阅读完毕,可以点点小手赞一下
发现错误,直接评论区中指正吧
橡皮擦的第 701 篇原创博客
从订购之日起,案例5年内保证更新
边栏推荐
猜你喜欢
Summary of three methods for SQL deduplication
医院管理系统数据库,课程设计,SQLserver,纯代码设计
Sql优化总结!详细!(2021最新面试必问)
Master SSR
Data Middle Office Construction (6): Data System Construction
Web系统常见安全漏洞介绍及解决方案-sql注入
“chmod 777-R 文件名”什么意思?
初识二叉搜索树
Redis缓冲穿透和缓冲击穿工具类的封装
Experience innovation and iteration through the development of a lucky draw applet
随机推荐
v-model的原理
初识二叉搜索树
Deletion of the sequence table
【23提前批】北森云计算-测开面经
Master SSR
【LeetCode】1161.最大层内元素和
【LeetCode】36.有效的数独
因存在自燃安全隐患,宝马7系和5系紧急召回,合计超过5.7万辆
odoo14 | 附件上传功能及实际使用
SQLSERVER merges subquery data into one field
C#多态的实现
Sql optimization summary!detailed!(Required for the latest interview in 2021)
NowCoderTOP28-34 binary tree - continuous update ing
"JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
Can I find a Go job in 7 days?Learn Go with arrays and pointers
NowCoderTOP17-22 Binary search/sort - continuous update ing
matlab 读取csv文件绘图
7 days to learn Go, Go structure + Go range to learn
MySQL中JOIN的用法
Many mock tools, this time I chose the right one