当前位置:网站首页>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
边栏推荐
猜你喜欢
Systrace 解析
【点云系列】Relationship-based Point Cloud Completion
PyTorch 10. Learning rate
RISCV MMU 概述
基于51单片机的体脂检测系统设计(51+oled+hx711+us100)
如何利用qemu搭建SOC protoype:80行代码实现一个Cortex M4 模拟器
Chapter 4 pytoch data processing toolbox
【点云系列】SO-Net:Self-Organizing Network for Point Cloud Analysis
Chapter 2 pytoch foundation 1
Chapter 1 numpy Foundation
随机推荐
AUTOSAR从入门到精通100讲(八十一)-AUTOSAR基础篇之FiM
excel实战应用案例100讲(八)-Excel的报表连接功能
PyTorch 18. torch. backends. cudnn
Int8 quantification and inference of onnx model using TRT
电力行业巡检对讲通信系统
CMSIS CM3源码注解
Wechat applet uses wxml2canvas plug-in to generate some problem records of pictures
AUTOSAR从入门到精通100讲(八十七)-高级EEA的关键利器-AUTOSAR与DDS
pth 转 onnx 时出现的 gather、unsqueeze 等算子
torch.where能否传递梯度
机器视觉系列(01)---综述
基于51单片机的三路超声波测距系统(定时器方式测距)
基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
enforce fail at inline_ container. cc:222
【点云系列】 场景识别类导读
Use originpro express for free
应急医疗通讯解决方案|MESH无线自组网系统
【点云系列】Relationship-based Point Cloud Completion
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
PyTorch 21. PyTorch中nn.Embedding模块