Logo

Programming-Idioms

History of Idiom 222 > diff from v32 to v33

Edit summary for version 33 by programming-idioms.org:
[Go] Fix comments: generics are coming

Version 32

2022-02-01, 22:40:35

Version 33

2022-08-19, 11:27:24

Idiom #222 Find the 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 the 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.

Variables
i,x,items
Variables
i,x,items
Extra Keywords
position
Extra Keywords
position
Code
i := -1
for j, e := range items {
	if e == x {
		i = j
		break
	}
}
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.
Comments bubble
Explicit loop, for your own strongly typed helper func.
Demo URL
https://play.golang.org/p/Z6F5vjMdluW
Demo URL
https://play.golang.org/p/Z6F5vjMdluW