当前位置:网站首页>【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)
}
边栏推荐
- Go obtains the processing results of goroutine through channel
- Word wrap: break word line feed is compatible with browsers
- Day012 application of one-dimensional array
- Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it
- Leetcode series (I): buying and selling stocks
- 火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。
- VScode连接远程服务器开发
- (posted) comparison of Eureka, consumer and Nacos 2
- Gbase 8C product introduction
- Book borrowing management system based on SSM
猜你喜欢

强网杯2021 pwn 赛题解析——babypwn

Gbase 8C product introduction

ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots

Talk about multimodality of fire

DNA coupled PbSe quantum dots | near infrared lead selenide PbSe quantum dots modified DNA | PbSe DNA QDs

Drools (5): drools advanced syntax

把Excel转换成CSV/CSV UTF-8

Leetcode series (I): buying and selling stocks

How to implement Devops with automation tools | including low code and Devops application practice

指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
随机推荐
指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
Basic statement of MySQL (1) - add, delete, modify and query
[Vani有约会]雨天的尾巴
“蔚来杯“2022牛客暑期多校训练营1
A Competitive Swarm Optimizer for Large Scale Optimization
MySQL query operation index optimization practice
Analysis of online and offline integration mode of o2o E-commerce
Netease Yunxin appeared at the giac global Internet architecture conference to decrypt the practice of the new generation of audio and video architecture in the meta universe scene
(转帖)eureka、consul、nacos的对比1
Interpretation of deepsort source code (V)
美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏
R2live code learning record (3): radar feature extraction
Interpretation of deepsort source code (VII)
网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...
Shell programming specifications and variables
Student achievement management system based on SSM
Gbase 8C core technology
Interpretation of deepsort source code (III)
Student status management system based on SSM
Golang controls the number of goroutines and obtains processing results