首页 > 其他分享 >[Go - slice] Remove Elements from a slice in Go

[Go - slice] Remove Elements from a slice in Go

时间:2024-02-06 09:15:13浏览次数:25  
标签:index Elements Remove slice champ Go elements

Go has a special indexing syntax that forms the basis of removing one or more elements from a slice. I will teach you this syntax and show you its various forms.

Based upon that, I'll show you how to remove a single element from a slice. However, you may find this somewhat limiting. I also will show you a more typical example of removing elements from a slice as we iterate over it. This technique shows you how to remove multiple elements from a slice.

func main() {
	all, err := loadChampions()
	if err != nil {
		log.Fatalf("An error occurred loading/parsing champions, err=%v", err)
	}
    
    // get 0 to 19
	some := all[0:20]
    // get 0 to 19
	some = all[:20]
    // get 10 to the end
	some = all[10:]
    // get all
	some = all[:]

	// Remove an element from the slice by index
    // ... spread operator
    // since append expects individual elements as its second and subsequent arguments, 
    // the ... operator is used to expand the second slice (all[11:]) into a series of arguments
    // So the 10th element is removed
	most := append(all[0:10], all[11:]...)
    
    // Remove element with new slice
    var filtered []champion
    for _, champ := range all {
        if !champ.hasClass("Sorcerer") {
            filtered = append(filtered, champ)
        }
    }

	// Remove elements by altering the slice in place
	index := 0
	for _, champ := range all {
		if champ.hasClass("Sorcerer") {
			continue
		}

		all[index] = champ
		index++
	}

	all = all[:index]
}

 

标签:index,Elements,Remove,slice,champ,Go,elements
From: https://www.cnblogs.com/Answer1215/p/18009087

相关文章

  • 2月摸鱼计划03 从并发编程本质了解Go高性能的本质
    1.0从并发编程本质了解Go高性能的本质1.1Goroutine协程可以理解为轻量级线程;Go更适合高并发场景原因之一:Go语言一次可以创建上万协成;“快速”:开多个协成打印。gofunc():在函数前加go代表创建协程;time.Sleep():协程阻塞,使主协程在子协程结束前阻塞不退出;乱序输出说......
  • client-go http trace分析耗时
    klog.InitFlags(nil)flag.Parse()deferklog.Flush()cfg,err:=clientcmd.BuildConfigFromFlags("","/root/.kube/config")iferr!=nil{ klog.Fatalf("Errorbuildingkubeconfig:%s",err.Error())}kubeClient,err:=kubern......
  • [转][Centos]安装 gogs
    也可以从https://gogs.io下载安装参考:https://blog.csdn.net/hahofe/article/details/1172919201、包管理安装#获取安装源sudowget-O/etc/yum.repos.d/gogs.repohttps://dl.packager.io/srv/gogs/gogs/main/installer/el/7.repo#开始下载安装sudoyuminstallgogs......
  • 在我的VS2019中重新配置2017项目生成的google test 项目
    原来的项目是其他版本的VS配置的,自己下载下来时候,本机也没有装GoogleTest所以用不起。如果重建项目在一个个引入工程代码太麻烦(文件多),所以我就想着有没有什么办法快捷配置,不用重建工程以下是我的一个配置方法,供大家交流学习:1.首先你本机要安装上GoogleTest,安装方法自查;2.如......
  • MongoDB配置用户账号与访问控制
    1.admin库创建root角色权限的用户db.createUser({user:"root",pwd:"123qwe",roles:["root"]})2.修改mongo配置文件,开启用户名密码连接验证security:authorization:enabled3.cmd关闭mongo服务netstopMongoDB4.cmd启动mongo服务netstartMongoDB直接localhost连接......
  • go 项目构建
    Go准备依赖环境:golang参考官网地址:https://golang.google.cn/dl/开发工具:可使用jetbrian的Go,但是需要付费,免费也就30天试用,没有社区版。Idea安装Go-plugin插件。插件地址:https://plugins.jetbrains.com/plugin/9568-go/versions/stable以Module形式导入应用因Go项目......
  • go-carbon v2.3.8 发布,轻量级、语义化、对开发者友好的 golang 时间处理库
    carbon是一个轻量级、语义化、对开发者友好的golang时间处理库,支持链式调用。目前已被awesome-go收录,如果您觉得不错,请给个star吧github.com/golang-module/carbongitee.com/golang-module/carbon安装使用Golang版本大于等于1.16//使用github库goget-ugithu......
  • go dns解析参数配置
    //lookup_dns.gopackagemainimport( "net" "time" "k8s.io/klog/v2")funcmain(){ dns:="kubernetes.default" if_,err:=net.LookupHost(dns);err!=nil{ klog.Errorf("lookup%sfailed,erris%v......
  • 在Go中使用接口:实用性与脆弱性的平衡货币的精度
    在Go中使用接口:实用性与脆弱性的平衡原创 TimLiu 爱发白日梦的后端 2024-02-0307:00 发表于广东 听全文爱发白日梦的后端专注Go语言领域的发展,学习成为更牛逼的架构师,日常分享Go语言、架构、软件工具的使用。168篇原创内容公众号点击上方“名......
  • UI自动化测试代码不想写脚本不想配?RunnerGo一键录制
    想快速配置可视化UI自动化测试脚本?RunnerGo近期上线脚本录制器,根据你的测试操作直接生成UI自动化测试脚本,下面是使用方法Step1:下载录制器点击RunnerGo上方插件按钮下载录制器Step2:录制器使用将插件文件拖入浏览器扩展程序点击打开录制器,在浏览器中进行操作时录制器会将操作录制为......