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 在切片中查找元素的主要内容,如果未能解决你的问题,请参考以下文章