How to find out element position in slice?

前端 未结 7 780
无人及你
无人及你 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:34

    Sorry, there's no generic library function to do this. Go doesn't have a straight forward way of writing a function that can operate on any slice.

    Your function works, although it would be a little better if you wrote it using range.

    If you happen to have a byte slice, there is bytes.IndexByte.

提交回复
热议问题