当前位置:网站首页>【golang学习笔记2.0】 golang中的数组和切片
【golang学习笔记2.0】 golang中的数组和切片
2022-07-27 06:05:00 【是誰萆微了承諾】
数组简介
- 数组可以存放多个同一类型数据,数组也是一种数据类型,在go中数组是值类型。
- 数组得定义: var 数组名称 [数组大小] 数据类型
go中得数组得使用注意事项
- 数组是多个相同类型数据组合,数组一旦定义了,其长度固定不变,不能动态变化。
- 数组中得元素是可以任何数据类型,包括值类型和引用类型,但是不能混用。
- 数组创建后 如果没有赋值。数组是有默认值得。int 默认0 string 默认空字符串"",bool类型 默认false
- 数组得下标是0开始得【基本大部分语言得数组都是下标0开始得】
- go得数组是值类型,在默认得情况瞎会进行值拷贝。数组之间不会影响
- 如果想在其他函数中,去修改原来得数组。可以使用引用传递。
切片的简介
- 切片数组得一个引用,因此切片得类型为引用类型,所以在值得传递时,遵循引用传递机制。
- 切片得使用和数组类似。切片得长度是可以变化的。所以可以理解为切片是定义一个空数组【类似于php中 $arr=array()】
- 切片定义的基本语法 var 变量名 [] 类型 ;例:var arr [] int
- 切片从底层上来说相当于一个结构体;
type slice struct{
ptr *[2]int
len int
cap int
}
切片的几种声明方式
- 第一种:声明一个未指定大小的数组来定义切片
package main
import (
"fmt"
)
func main() {
//声明一个未指定大小的数组来定义切片
var slice []int
//直接这样赋值是不行的,会报索引超出范围的错误
// slice[0] =4
//这种情况需要append函数来追加数据
slice = append(slice, 1)
slice = append(slice, 2)
slice = append(slice, 3)
//可以这样在已有的索引范围内重新赋值,slice[3]=5 这样是不行的。
slice[2] =4
//打印结果 slice: [1 2 4]
fmt.Printf("slice: %v\n", slice)
}
- 第二种:使用make函数来声明一个切片
package main
import (
"fmt"
)
func main() {
//声明一个未指定大小的数组来定义切片
//make(切片的类型,切片的长度,切片的容量)
var slice []int=make([]int, 10,20)
//打印结果:slice: [0 0 0 0 0 0 0 0 0 0]
fmt.Printf("slice: %v\n", slice)
}
- 第三种:使用make函数来声明一个切片
package main
import (
"fmt"
)
func main() {
//直接进行赋值;类似于make
var slice []int=[]int{
1,2,3}
//打印结果:slice: [1 2 3]
fmt.Printf("slice: %T\n", slice)
}
- 第四种:引用数组的方式创建切片
package main
import (
"fmt"
)
func main() {
//引用数组的形式
var arr [6]int = [...]int{
1, 2, 3, 4, 5, 6}
slice := arr[1:4]
//打印结果:slice: [2 3 4]
fmt.Printf("slice: %v\n", slice)
}
边栏推荐
- Digital image processing - Chapter 6 color image processing
- Vscode connection remote server development
- 指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
- 请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
- “蔚来杯“2022牛客暑期多校训练营1
- Relevant principles of MySQL index optimization
- AI: play games in your spare time - earn it a small goal - [Alibaba security × ICDM 2022] large scale e-commerce map of risk commodity inspection competition
- Drools(5):Drools基础语法(3)
- 线性表 -- 栈和队列
猜你喜欢

Automatically generate UML sequence diagram according to text (draw.io format)

How to learn C language? This article gives you the complete answer

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces

Bert and RESNET can also be trained on mobile phones?!

Book borrowing management system based on SSM

使用反射实现动态修改@Excel的注解属性

VScode连接远程服务器开发

Qi Yue: thiol modified oligodna | DNA modified cdte/cds core-shell quantum dots | DNA coupled indium arsenide InAs quantum dots InAs DNA QDs

把Excel转换成CSV/CSV UTF-8

C4D动画如何提交云渲染农场快速渲染?
随机推荐
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Student achievement management system based on SSM
A Competitive Swarm Optimizer for Large Scale Optimization
指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
MySQL optimization SQL related (continuous supplement)
DNA coupled PbSe quantum dots | near infrared lead selenide PbSe quantum dots modified DNA | PbSe DNA QDs
Gbase 8C - SQL reference 6 SQL syntax (13)
Vscode connection remote server development
Gbase 8C technical features
Drools (5): drools basic syntax (3)
Relevant principles of MySQL index optimization
MySQL quickly compares database table data
Instruction set x digital technology accelerates the digital transformation of government and enterprises, and builds Unicorn enterprise alliance in DT field
Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort
内部类--看这篇就懂啦~
Summary of APP launch in vivo application market
Leetcode series (I): buying and selling stocks
MySQL index failure and solution practice
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm