当前位置:网站首页>Go language learning notes - slice, map | go language from scratch
Go language learning notes - slice, map | go language from scratch
2022-04-23 09:18:00 【Swordsman a Liang_ ALiang】
Catalog
Learning notes , Where is where .
1、Slice section
Slice and java Medium ArrayList similar ,Go The length of the language array cannot be changed ,Slice Its length is not fixed , Appendable element .
The definition can be :
var identifier []type
You can also use make function
var slice1 []type = make([]type, len)
The sample code is as follows
package main
import "fmt"
// section
func test_slice() {
a := [...]int{1, 2, 3}
b := a[:]
fmt.Printf("a:%T,b%T\na=%v,b=%v\n", a, b, a, b)
c := a[:2]
fmt.Println(c)
s := make([]string, 0)
s = append(s, "haha")
s = append(s, "haha1", "haha2")
fmt.Println(s, len(s), cap(s))
printSlice := func(x []int) { fmt.Printf("len=%d,cap=%d,slice=%v\n", len(x), cap(x), x) }
var d []int
if d == nil {
printSlice(d)
}
copyS := make([]string, cap(s)*2)
copy(copyS, s)
fmt.Println(copyS, s)
}
func main() {
test_slice()
}
Execution results
a:[3]int,b[]int
a=[1 2 3],b=[1 2 3]
[1 2]
[haha haha1 haha2] 3 3
len=0,cap=0,slice=[]
[haha haha1 haha2 ] [haha haha1 haha2]
Be careful
1、 It can be seen that a It's an array ,b Yes, it will a Array to slice .
2、c yes a The index of the array is 0 To 2-1 The elements of , As a slice .
3、 have access to copy function , Copy slice elements .
4、 Use len()、cap() Built in functions , You can get the length and maximum capacity of the slice .
5、 Use append() Function can append slice elements .
2、Map
and java Medium map similar , Creating map You also need to specify the key value type .
Define the way :
var map_variable map[key_data_type]value_data_type
map_variable := make(map[key_data_type]value_data_type)
The sample code is as follows
package main
import "fmt"
func testMap() {
var a = make(map[string]int)
a[" Xiao Huang "] = 10
a[" Xiaohong "] = 18
for s, i := range a {
fmt.Println(s, i)
}
b, ok := a[" Xiaolan "]
fmt.Println(b, ok)
a[" floret "] = 21
delete(a, " Xiao Huang ")
fmt.Println(a)
}
func main() {
testMap()
}
Execution results
Xiao Huang 10
Xiaohong 18
0 false
map[ Xiaohong :18 floret :21]
Be careful
1、 In obtaining map At a certain value , Will return two return values , The second return value is bool type , If the key value pair does not exist , Then for false.
2、 have access to delete() Function to delete key value pairs .
Summary
The way of use is also relatively rigorous , They are more conventional data types in work .
版权声明
本文为[Swordsman a Liang_ ALiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230633437818.html
边栏推荐
- valgrind和kcachegrind使用運行分析
- 653. 两数之和 IV - 输入 BST
- Go language self-study series | golang nested structure
- AQS & reentrantlock implementation principle
- Detailed explanation of delete, truncate and drop principles in MySQL database
- EmuElec 编译总结
- Get trustedinstaller permission
- Wechat: get the owner of a single tag
- web页面如何渲染
- Project upload part
猜你喜欢

Failed to download esp32 program, prompting timeout

653. 两数之和 IV - 输入 BST

ATSS(CVPR2020)

MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)

Pctp test experience sharing

Multi view depth estimation by fusing single view depth probability with multi view geometry

nn. Explanation of module class

valgrind和kcachegrind使用运行分析

Number of islands

Kettle实验 (三)
随机推荐
Colorui solves the problem of blocking content in bottom navigation
Principle of synchronized implementation
基于ThinkPHP5版本TRC20-资金归集解决方案
Project upload part
Harbor enterprise image management system
【SQL server速成之路】数据库的视图和游标
考研线性代数常见概念、问题总结
A must see wechat applet development guide 1 - basic knowledge
【原创】使用System.Text.Json对Json字符串进行格式化
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
Applet in wechat and app get current ()
About CIN, scanf and getline, getchar, CIN Mixed use of getline
[C language] document operation
Resource packaging dependency tree
Redis Desktop Manager for Mac
搞不懂时间、时间戳、时区,快来看这篇
On array replication
SAP 101K 411K 库存变化
OpenCV中的图像处理 —— 轮廓入门+轮廓特征
Thread scheduling (priority)