golang 在切片中查找元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 在切片中查找元素相关的知识,希望对你有一定的参考价值。

func find(array []interface{}, item interface{}) (index int) {
	index = -1
	for idx, value := range array {
		if value == item {
			index = idx
			break
		}
	}
	return
}

golang 从切片中删除元素

func remove(array []interface{}, index int) []interface{} {
	if index < 0 || index > len(array) {
		return array
	}
	return append(array[0:index], array[index+1:]...)
}

以上是关于golang 在切片中查找元素的主要内容,如果未能解决你的问题,请参考以下文章

golang 使用Go查找给定切片中的最大字符串数

使用接口在非空数组(切片)中查找另一个“特殊”数组

golang 将元素添加到切片中

golang 从切片中删除元素

golang中切片的cap vs len

Golang理解-数组和切片