当前位置:网站首页>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
边栏推荐
- x86架构初探之8086
- 网络层重要知识(面试、复试、期末)
- . net encountered failed to decode downloaded font while loading font:
- Int8 quantification and inference of onnx model using TRT
- EasyUI combobox determines whether the input item exists in the drop-down list
- Device Tree 详解
- 【点云系列】Relationship-based Point Cloud Completion
- excel实战应用案例100讲(八)-Excel的报表连接功能
- GIS实战应用案例100篇(三十四)-拼接2020globeland30
- 基于Labview上位机的51单片机步进电机控制系统(上位机代码+下位机源码+ad原理图+51完整开发环境)
猜你喜欢

WinForm scroll bar beautification

北峰油气田自组网无线通信对讲系统解决方案

MySQL installation and configuration - detailed tutorial

Chapter 2 pytoch foundation 1

Are realrange and einsum really elegant

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

Résolution du système

AUTOSAR从入门到精通100讲(八十六)-UDS服务基础篇之2F

ARMCC/GCC下的stack protector

Chapter 8 generative deep learning
随机推荐
AUTOSAR从入门到精通100讲(八十三)-BootLoader自我刷新
【点云系列】Relationship-based Point Cloud Completion
带您遨游太空,美摄科技为航天创意小程序提供全面技术支持
基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
PyTorch 19. Differences and relations of similar operations in pytorch
UEFI学习01-ARM AARCH64编译、ArmPlatformPriPeiCore(SEC)
scons 搭建嵌入式arm编译
【技术规范】:如何写好技术文档?
Draw margin curve in arcface
Pep517 error during pycuda installation
美摄科技起诉天目传媒使用火山引擎侵权代码的声明
STM32多路测温无线传输报警系统设计(工业定时测温/机舱温度定时检测等)
【指标】Precision、Recall
torch.where能否传递梯度
The simplest and complete example of libwebsockets
北峰通信助力湛江市消防支队构建PDT无线通信系统
AUTOSAR从入门到精通100讲(五十)-AUTOSAR 内存管理系列- ECU 抽象层和 MCAL 层
PyTorch 20. PyTorch技巧(持续更新)
Wechat applet uses wxml2canvas plug-in to generate some problem records of pictures
公专融合对讲机是如何实现多模式通信下的协同工作?