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,
}

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

查看所有标签

猜你喜欢:

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

Agile Web Development with Rails 4

Agile Web Development with Rails 4

Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2013-10-11 / USD 43.95

Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details. Tens of thousands of deve......一起来看看 《Agile Web Development with Rails 4》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具