Standard library / slices

slices.Index()

Returns the first index of an equal element or -1 if absent. Expected stdout: '0 -1\n'.

Formal Syntax & Structure

go syntaxgo
func Index[S ~[]E, E comparable](s S, v E) int

Parameters & Attributes

NameTypeDescription
sSInput slice; may share its backing array.
vEElement value or variadic values for this operation.

Return Value

int

Returns the first index of an equal element or -1 if absent.

Working Example

Example Snippetgo
package main
import (  "fmt"  "slices")
func main() {  fmt.Println(slices.Index([]int{4, 8, 4}, 4), slices.Index([]int{4, 8}, 9))}
Go Reference