Logo

Programming-Idioms

History of Idiom 222 > diff from v33 to v34

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

Version 33

2022-08-19, 11:27:24

Version 34

2022-08-19, 11:30:41

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
Imports
import "golang.org/x/exp/slices"
Code
i := slices.Index(items, x)
Comments bubble
This generic func slices.Index works for all slice types
Doc URL
https://pkg.go.dev/golang.org/x/exp/slices#Index
Demo URL
https://go.dev/play/p/EBF1O2eofa1