How to find out element position in slice?

前端 未结 7 777
无人及你
无人及你 2020-12-07 11:20

How does one determine the position of an element present in slice?

I need something like the following:

type intSlice []int

func (slice intSlice) p         


        
7条回答
  •  醉酒成梦
    2020-12-07 11:30

    func index(slice []string, item string) int {
        for i, _ := range slice {
            if slice[i] == item {
                return i
            }
        }
        return -1
    }
    

提交回复
热议问题