Logo

Programming-Idioms

History of Idiom 222 > diff from v1 to v2

Edit summary for version 2 :
New Go implementation by user [programming-idioms.org]

Version 1

2020-04-06, 21:27:57

Version 2

2020-04-06, 21:32:49

Idiom #222 Find first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Idiom #222 Find first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Extra Keywords
position
Extra Keywords
position
Code
i := -1
for j, e := range items {
	if e == x {
		i = j
		break
	}
}
Comments bubble
There is no generic func for this, consider an explicit loop in your own strongly typed helper func.
Demo URL
https://play.golang.org/p/Z6F5vjMdluW