首页 > 其他分享 >Go - Remove values from a slice

Go - Remove values from a slice

时间:2023-10-07 23:14:00浏览次数:36  
标签:slice 159 53 element int values numbers Go

To take out the first element of the slice:

numbers   :=   [] int { 3 ,   14 ,   159 ,   26 ,   53 ,   58 } 
numbers   =   numbers [ 1 :]   //  remove  element  0

To take out the last element of the slice:

numbers   :=   [] int { 3 ,   14 ,   159 ,   26 ,   53 ,   58 } 
numbers   =   numbers [: len ( numbers ) - 1 ]   //  remove  last  element

Removing elements in between two adjacent elements within a slice is quite straightforward too. You simply append the head of the original slice with the tail of the original slice, removing whatever is in between. In this case, you want to remove the element at index 2, which is 159:

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

 

标签:slice,159,53,element,int,values,numbers,Go
From: https://www.cnblogs.com/zhangzhihui/p/17747706.html

相关文章

  • Go - Insert values into a slice
    Thereisnobuilt-infunctionforinsertion,butyoucanstilluseappendforthetask.Let’ssayyouwanttoinsertthenumber1000betweenelementsatindex2and3,whichareints159and26,respectively:numbers:=[]int{3,14,159,......
  • 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......