首页 > 其他分享 >Go - Insert values into a slice

Go - Insert values into a slice

时间:2023-10-07 22:55:05浏览次数:36  
标签:Insert slice int into tail numbers want Go append

There is no built - in function for insertion, but you can still use append for the task. Let’s say you want to insert the number 1000 between elements at index 2 and 3, which are ints 159 and 26, respectively:

numbers   :=   [] int { 3 ,   14 ,   159 ,   26 ,   53 ,   58 } 
numbers   =   append ( numbers [: 2 + 1 ],   numbers [ 2 :] ... ) 
numbers [ 3 ]   =   1000

 

What if you want to add an element to the beginning of the slice; for example, you want to add the integer 2000 to the beginning of the slice.

numbers   =   append ([] int { 2000 },   numbers ... )

 

What if you want to add a slice of numbers in between two elements of another slice of numbers? For example, you want to insert the slice []int{1000, 2000, 3000, 4000} in between index 2 and 3 of the numbers slice like before.

There are a few ways of doing this, but stick with using append , which is one of the shortest ways:

numbers   =   [] int { 3 ,   14 ,   159 ,   26 ,   53 ,   58 } 
inserted   :=   [] int { 1000 ,   2000 ,   3000 ,   4000 } 

tail   :=   append ([] int {},   numbers [ 3 :] ... ) 
numbers   =   append ( numbers [: 3 ],   inserted ... ) 
numbers   =   append ( numbers ,   tail ... )

First of all, you need to create another slice, tail , to store the tail part of the original slice. You can’t simply slice it and store it into another variable (this is called shallow copy ), because slices are not arrays: they are a pointer to a part of the array and its length. If you slice numbers and store it in tail , when you change numbers , tail will also change, and that is not what you want. Instead, you want to create a new slice by appending it to an empty slice of ints.

标签:Insert,slice,int,into,tail,numbers,want,Go,append
From: https://www.cnblogs.com/zhangzhihui/p/17747695.html

相关文章

  • Go - Defining Metadata for Struct Fields
    Problem: Youwanttodefinemetadatatodescribethestructfields.Solution: Usestructtagstodefinemetadataandthereflectpackagetoaccessthetags. Oneverycommonplaceyoufindthisisinthejsonpackage:typePersonstruct{......
  • CF1856B Good Arrays
    题意简述:给定一个序列\(a\),我们定义一个序列\(b\)是好的当且仅当对于\(1\dotsn\)内的每一个\(i\),\(a_i\neqb_i\)且\(\sum_{i=1}^na_i=\sum_{i=1}^nb_i\)(\(a_i\),\(b_i\)均为正整数)。现在有\(T\)组数据,每组数据给定\(n\)和序列\(a\),判断是否存在一个合法的序......
  • Go 项目代码布局
    Go项目代码布局目录Go项目代码布局一、Go语言“创世项目”结构1.1src目录结构三个特点二、Go项目布局演进2.1演进一:Go1.4版本删除pkg这一中间层目录并引入internal目录2.2演进二:Go1.6版本增加vendor目录2.3演进三:Go1.13版本引入go.mod和go.sum三、现在Go......
  • Go 基础之基本数据类型
    Go基础之基本数据类型目录Go基础之基本数据类型一、整型1.1平台无关整型1.1.1基本概念1.1.2分类有符号整型(int8~int64)无符号整型(uint8~uint64)1.2平台相关整型1.2.1基本概念1.2.2注意点1.2.3获取三个类型在目标运行平台上的长度1.3整型的溢出问题1.3.1什么是整形溢出?1.......
  • GO语言基础之基本运算符
    GO语言基础之基本运算符目录GO语言基础之基本运算符一、运算符内置运算符:二、算术运算符三、关系运算符四、逻辑运算符五、位运算符六、赋值运算符一、运算符作用:运算符用于在程序运行时执行数学或逻辑运算。内置运算符:Go语言内置的运算符有:算术运算符关系运算符逻辑运......
  • Go基础之指针
    Go语言中的指针目录Go语言中的指针一、Go语言中的指针介绍1.1指针介绍1.2基本语法1.3声明和初始化1.4Go指针的3个重要概念1.4.1指针地址(PointerAddress)1.4.2指针类型(PointerType)1.4.3指针取值(PointerDereferencing)1.5获取指针的地址和解引用1.6传递指针给函数1.7指......
  • Go 复合数据类型之结构体与自定义类型
    Go复合数据类型之结构体与自定义类型目录Go复合数据类型之结构体与自定义类型一、类型别名和自定义类型1.1类型定义(TypeDefinition)简单示例1.2类型别名简单示例1.3类型定义和类型别名的区别二、结构体2.1结构体介绍2.2结构体的定义2.3定义一个空结构体2.3.1空结构体介......
  • Go基础之变量和常量
    Go基础之变量和常量目录Go基础之变量和常量一.标识符、关键字、内置类型和函数1.1标识符1.2关键字1.3保留字1.4内置类型1.4.1值类型:1.4.2引用类型:(指针类型)1.5内置函数1.6内置接口error二.Go变量命名规范2.1采用驼峰体命名2.2简单、短小为首要原则2.3变量名字中不要......
  • Django实战项目-学习任务系统-用户登录
    第一步:先创建一个Django应用程序框架代码1,先创建一个Django项目django-adminstartprojectmysite将创建一个目录,其布局如下:mysite/manage.pymysite/__init__.pysettings.pyurls.pyasgi.pywsgi.py2,再创建一个Dja......
  • GoLang context包
    初始化一个context如果确定是开头则用Background,如果不确定则用ToDocontext包核心API有四个1.context.WithValue设置键值对,并且返回一个新的context实例2.context.WithCancel3.context.WithDeadline4.context.WithTimeout三者都返回一个可取消的context实例和取消函数,WithTi......