当前位置:网站首页>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
边栏推荐
- EasyUI combobox determines whether the input item exists in the drop-down list
- Chapter 8 generative deep learning
- Swin transformer to onnx
- Chapter 4 pytoch data processing toolbox
- Visual studio 2019 installation and use
- imx6ull-qemu 裸机教程1:GPIO,IOMUX,I2C
- 【技术规范】:如何写好技术文档?
- 海南凤凰机场智能通信解决方案
- excel实战应用案例100讲(八)-Excel的报表连接功能
- armv8m(cortex m33) MPU实战
猜你喜欢

Chapter 8 generative deep learning

Paddleocr image text extraction

【51单片机交通灯仿真】

AUTOSAR从入门到精通100讲(八十一)-AUTOSAR基础篇之FiM

【无标题】制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码

直观理解 torch.nn.Unfold

Gather, unsqueeze and other operators when PTH is converted to onnx

GIS实战应用案例100篇(五十三)-制作三维影像图用以作为城市空间格局分析的底图

项目文件“ ”已被重命名或已不在解决方案中、未能找到与解决方案关联的源代码管理提供程序——两个工程问题

Draw margin curve in arcface
随机推荐
armv8m(cortex m33) MPU实战
北峰油气田自组网无线通信对讲系统解决方案
PyTorch 22. PyTorch常用代码段合集
Warning "force fallback to CPU execution for node: gather_191" in onnxruntime GPU 1.7
机器学习——朴素贝叶斯
使用 trt 的int8 量化和推断 onnx 模型
Device Tree 详解
被 onnx.checker.check_model 检查出的常见错误
x509解析
使用proteus仿真STM32超声波SRF04测距!Code+Proteus
Intuitive understanding of torch nn. Unfold
PyTorch 14. Module class
By onnx checker. check_ Common errors detected by model
多机多卡训练时的错误
Cmder Chinese garbled code problem
Detailed explanation of unwind stack backtracking
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
无盲区、长续航|公专融合对讲机如何提升酒店服务效率?
PyTorch 20. PyTorch技巧(持续更新)
Pymysql connection database