当前位置:网站首页>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
lenIs 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 (10)
- Leetcode学习计划之动态规划入门day3(198,213,740)
- PHP PDO ODBC将一个文件夹的文件装载到MySQL数据库BLOB列,并将BLOB列下载到另一个文件夹
- 【递归之数的拆分】n分k,限定范围的拆分
- 现在做自媒体能赚钱吗?看完这篇文章你就明白了
- Go语言条件,循环,函数
- Connect PHP to MSSQL via PDO ODBC
- 开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
- Recommended search common evaluation indicators
- Node. JS ODBC connection PostgreSQL
猜你喜欢
随机推荐
php类与对象
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
通过 PDO ODBC 将 PHP 连接到 MySQL
Upgrade MySQL 5.1 to 5.67
木木一路走好呀
Treatment of idempotency
Summary of interfaces for JDBC and servlet to write CRUD
Multi level cache usage
Mysql database explanation (10)
多级缓存使用
pgpool-II 4.3 中文手册 - 入门教程
Codejock Suite Pro v20. three
What if the server is poisoned? How does the server prevent virus intrusion?
cadence SPB17.4 - Active Class and Subclass
fatal error: torch/extension. h: No such file or directory
How to test mobile app?
Openstack theoretical knowledge
通過 PDO ODBC 將 PHP 連接到 MySQL
s16.基于镜像仓库一键安装containerd脚本
Configuration of multi spanning tree MSTP









