当前位置:网站首页>go语言切片操作
go语言切片操作
2022-04-23 06:15:00 【玩哈哈527】
package main
import (
"fmt"
)
func main() {
//创建切片,使用切片
slice:=make([]string,5)//长度5,默认容量也是5
//slice1:=make([]string,5,3)//长度5大于容量3,错误
slice2:=make([]int,3,5)//创建整型切片,长度3容量5
slice3:=[]string{
"red","blue","yellow"}//初始化
slice4:=[]int{
3,4,5,6}//初始化整型切片
slice5:=[]string{
19:"test"}//用字符串test初始化第20个元素
//如果在[]运算符里指定了一个值,那么创建的就是数组而不是切片。只有不指定值
//的时候,才会创建切片,
array:=[3]int{
1,2,3}//数组
slice6:=[]int{
4,5,6}//切片
//创建nil切片
var slice7 []int
//创建空切片
slice8:=make([]int,0)
slice9:=[]int{
}
// 创建一个整型切片
// 其长度和容量都是 5 个元素
slice_a := []int{
10, 20, 30, 40, 50}
slice_b:=slice_a[1:3]
fmt.Print(slice_a)
fmt.Println(slice_b)
slice_b[1]=35
newslice:=append(slice_a,60)
fmt.Print(slice)
//fmt.Print(slice1)
fmt.Print(slice2)
fmt.Print(slice3)
fmt.Println(slice4)
fmt.Println(slice5)
fmt.Println(array)
fmt.Println(slice6)
fmt.Print(slice7)
fmt.Print(slice8)
fmt.Println(slice9)
fmt.Print(slice_a)
fmt.Print(slice_b)
fmt.Print(newslice)
// 创建两个切片,并分别用两个整数进行初始化
s1 := []int{
1, 2}
s2 := []int{
3, 4}
fmt.Printf("%v\n",append(s1,s2...))//使用...运算符,可以将一个切片的所有元素追加到另一个切片里
创建一个整型切片 其长度和容量都是 4 个元素
slice_c := []int{
10, 20, 30, 40}
// 迭代每一个元素,并显示其值
for index,value:=range slice_c {
fmt.Printf("index: %d,value: %d\n",index,value)
}
创建一个整型切片的切片,多维切片
mul_slice := [][]int{
{
10}, {
100, 200}}
fmt.Println(mul_slice)
mul_slice[0]=append(mul_slice[0],30)
fmt.Println(mul_slice)
分配包含 100 万个整型值的切片
slice10:=make([]int,10)
将 slice10 传递到函数 foo
slice10=foo(slice10)
fmt.Println(slice10)
}
func foo(slice []int) []int {
return slice
}
/* 打印输出结果 [10 20 30 40 50][20 30] [ ][0 0 0][red blue yellow][3 4 5 6] [ test] [1 2 3] [4 5 6] [][][] [10 20 35 40 50][20 35][10 20 35 40 50 60][1 2 3 4] index: 0,value: 10 index: 1,value: 20 index: 2,value: 30 index: 3,value: 40 [[10] [100 200]] [[10 30] [100 200]] [0 0 0 0 0 0 0 0 0 0] */
版权声明
本文为[玩哈哈527]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_28058509/article/details/119213256
边栏推荐
- PyTorch 10. Learning rate
- . net encountered failed to decode downloaded font while loading font:
- Error in multi machine and multi card training
- 带您遨游太空,美摄科技为航天创意小程序提供全面技术支持
- ECDSA 签名验证原理及C语言实现
- scons 搭建嵌入式arm编译
- Infrared sensor control switch
- The simplest and complete example of libwebsockets
- 防汛救灾应急通信系统
- 直观理解 torch.nn.Unfold
猜你喜欢
随机推荐
安装 pycuda 出现 PEP517 的错误
Use originpro express for free
enforce fail at inline_container.cc:222
美摄科技起诉天目传媒使用火山引擎侵权代码的声明
ECDSA 签名验证原理及C语言实现
美摄科技受邀LVSon2020大会 分享《AI合成虚拟人物的技术框架与挑战》
LPDDR4笔记
Résolution du système
Unwind 栈回溯详解
armv8m(cortex m33) MPU实战
Gather, unsqueeze and other operators when PTH is converted to onnx
机器视觉系列(01)---综述
unhandled system error, NCCL version 2.7.8
Systrace 解析
Pymysql connection database
onnxruntime-gpu 1.7 出现的警告“Force fallback to CPU execution for node: Gather_191”等
AUTOSAR从入门到精通100讲(五十二)-诊断和通信管理功能单元
PyTorch 12. hook的用法
swin transformer 转 onnx
城市应急管理|城市突发事故应急通信指挥调度系统


![[point cloud series] pnp-3d: a plug and play for 3D point clouds](/img/83/3662bc668602110236e43ee0b9d7ac.png)






