golang数据结构之slice篇

栏目: Go · 发布时间: 7年前

package main

import "github.com/sanity-io/litter"

// slice增删改查,很实用
func main() {
	var sliceInt []int
	// init
	sliceInt = append(sliceInt, []int{1, 2, 3, 4}...)
	litter.Dump(sliceInt)
	// remove index 0
	sliceInt = append(sliceInt[:0], sliceInt[1:]...)
	litter.Dump(sliceInt)
	sliceInt = append(sliceInt, []int{5,6,7,8,9,10}...)
	litter.Dump(sliceInt)
	// remove elem when iterator
	for i:= len(sliceInt)-1; i >=0; i-- {
		if sliceInt[i] % 2 == 0 {
			sliceInt = append(sliceInt[:i], sliceInt[i+1:]...)
		}
	}
	litter.Dump(sliceInt)
	// update elem
	sliceInt[len(sliceInt)-1] = 10
	litter.Dump(sliceInt)
}
output
[]int{
  1,
  2,
  3,
  4,
}
[]int{
  2,
  3,
  4,
}
[]int{
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
}
[]int{
  3,
  5,
  7,
  9,
}
[]int{
  3,
  5,
  7,
  10,
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of UNIX Programming

The Art of UNIX Programming

Eric S. Raymond / Addison-Wesley / 2003-10-3 / USD 54.99

Writing better software: 30 years of UNIX development wisdom In this book, five years in the making, the author encapsulates three decades of unwritten, hard-won software engineering wisdom. Raymond b......一起来看看 《The Art of UNIX Programming》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具