当前位置:网站首页>Go language slice, range, set
Go language slice, range, set
2022-04-23 15:41:00 【The end of the world and you】
Go Language slice , Range , aggregate
1.Go Language slice (Slice)
summary
Go Language slicing is the abstraction of arrays .
Go The length of the array cannot be changed , In a particular scenario, such a collection is not applicable ,Go Provides a kind of flexibility , Powerful built-in type slicing (“ The dynamic array ”), Compared with array, the length of slice is not fixed , You can append elements , In addition, it may increase the volume of slices .
Defining slices
You can define slices by declaring an array of unspecified size :
var identifier []type
var si []int
Or use make()
Function to create a slice :
var slice1 []type = make([]type, len)
Or we could just write it as
slice1 := make([]type, len)
Capacity can also be specified , among capacity
Is an optional parameter .
make([]T, length, capacity)
here
len
Is the length of the array and is also the initial length of the slice .
Slice initialization
s :=[] int {
1,2,3 }
len() and cap() function
Slices are indexable , And can be len()
Method to get the length .
Slicing provides a way to calculate capacity cap()
You can measure how long a slice can be .
package main
import "fmt"
func main() {
var numbers = make([]int, 3, 5)
fmt.Println(len(numbers), cap(numbers), numbers)
}
-----------------------
Output :
3 5 [0 0 0]
empty (nil
) section
A slice defaults to... Before initialization
nil
, The length is 0, Examples are as follows :
package main
import "fmt"
func main() {
var numbers []int
if numbers == nil {
fmt.Println(" Slice is empty ")
}
}
--------------------------
Output : Slice is empty
Slice off
package main
import "fmt"
func main() {
numbers := []int{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(numbers)
fmt.Println(numbers[1:4])
fmt.Println(numbers[:3])
fmt.Println(numbers[4:])
}
------------------------------
Output :
[0 1 2 3 4 5 6 7 8 9]
[1 2 3]
[0 1 2]
[4 5 6 7 8 9]
append() and copy() function
append()
package main
import "fmt"
func main() {
numbers := []int{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
/* Add an element to the slice */
numbers = append(numbers, 521)
/* Add multiple elements at the same time */
numbers = append(numbers, 11, 12, 13)
fmt.Println(numbers)
}
---------------------------------------------------
Output :
[0 1 2 3 4 5 6 7 8 9 521 11 12 13]
copy()
package main
import "fmt"
func main() {
numbers := []int{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
/* Create slices numbers1 Twice the capacity of the previous slice */
numbers1 := make([]int, len(numbers), (cap(numbers))*2)
copy(numbers1, numbers)
fmt.Println(numbers1)
}
-----------------------------------------------
Output :
[0 1 2 3 4 5 6 7 8 9]
2.Go Language range (Range)
Go In language range
Keywords are used for for
Iterating arrays in a loop (array
)、 section (slice
)、 passageway (channel
) Or set (map
) The elements of . In the array and slice, it returns the index of the element and the value corresponding to the index , Return... In the collection key-value
Yes .
example 1:
package main
import "fmt"
func main() {
// Use range Ask for one slice And . Using arrays is very similar to this
nums := []int{
2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println(sum)
}
-----------------------------
Output :9
example 2:
package main
import "fmt"
func main() {
// Use range Ask for one slice And . Using arrays is very similar to this
nums := []int{
2, 3, 4}
sum := 0
for i, num := range nums {
sum += num
fmt.Println(i)
}
fmt.Println(sum)
}
----------------------------------
Output :
0
1
2
9
example 3:range
It can also be used to enumerate Unicode character string . The first parameter is the index of the character , The second is the character (Unicode Value ) In itself .
package main
import "fmt"
func main() {
for i, c := range "hello" {
fmt.Println(i, c)
}
}
---------------------------------------
Output :
0 104
1 101
2 108
3 108
4 111
3.Go Language Map( aggregate )
Map
Is an unordered set of key value pairs .Map
The most important point is through key
To quickly retrieve data ,key
Similar to index , The value that points to the data .
Map
It's a collection , So we can iterate it like we iterate arrays and slices . however ,Map
Is chaotic , We can't decide the return order , This is because Map
It's using hash
Table to achieve .
example 1:
package main
import "fmt"
func main() {
var country map[string]string
country = make(map[string]string)
/* map Insert key - value Yes */
country["France"] = " In Paris, "
country["beijing"] = " Beijing "
country["Japan"] = " Japan "
/* Use the key to output map values */
for cou := range country {
fmt.Println(cou, "+", country[cou])
}
}
-------------------------------------------------
Output :
France + In Paris,
beijing + Beijing
Japan + Japan
example 2:
package main
import "fmt"
func main() {
var country map[string]string
country = make(map[string]string)
/* map Insert key - value Yes */
country["France"] = " In Paris, "
country["beijing"] = " Beijing "
country["Japan"] = " Japan "
/* See if the element exists in the collection */
capital, ok := country["American"]
if ok {
fmt.Println(capital)
} else {
fmt.Println("American There is no capital of ")
}
}
----------------------------------------
Output :
American There is no capital of
example 3:delete()
Function to delete the elements of a collection
package main
import "fmt"
func main() {
var country map[string]string
country = make(map[string]string)
/* map Insert key - value Yes */
country["France"] = " In Paris, "
country["beijing"] = " Beijing "
country["Japan"] = " Japan "
delete(country, "Japan")
fmt.Println(country)
}
------------------------------------
Output :map[France: In Paris, beijing: Beijing ]
Copyright notice : This tutorial is based on the rookie tutorial
版权声明
本文为[The end of the world and you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231533523977.html
边栏推荐
猜你喜欢
Mysql database explanation (8)
字节面试 transformer相关问题 整理复盘
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
Application of Bloom filter in 100 million flow e-commerce system
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Multitimer V2 reconstruction version | an infinitely scalable software timer
移动金融(自用)
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
单体架构系统重新架构
随机推荐
大型互联网为什么禁止ip直连
Timing model: gated cyclic unit network (Gru)
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
CAP定理
时序模型:长短期记忆网络(LSTM)
JVM-第2章-类加载子系统(Class Loader Subsystem)
Mysql database explanation (VII)
What role does the software performance test report play? How much is the third-party test report charged?
【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
Leetcode学习计划之动态规划入门day3(198,213,740)
Load Balancer
基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
时序模型:门控循环单元网络(GRU)
Modèle de Cluster MySQL et scénario d'application
Sorting and replying to questions related to transformer
What if the server is poisoned? How does the server prevent virus intrusion?
Openstack command operation
Multitimer V2 reconstruction version | an infinitely scalable software timer
Go语言切片,范围,集合
现在做自媒体能赚钱吗?看完这篇文章你就明白了