Skip to content

proposal: strings: add ListIndex #16774

@256dpi

Description

@256dpi

While reading the source code of many packages lately I stumbled a lot over the following function declaration:

func stringInList(list []string, str string) bool {
    for _, val := range list {
        if val == str {
            return true
        }
    }

    return false
}

I have the feeling that searching a slice of strings for the first occurrence of a string is a very common need and might be a good addition to the standard library.

I would propose the following function for the strings package:

func ListIndex(list []string, str string) int {
    for i, val := range list {
        if val == str {
            return i
        }
    }

    return -1
}

I know adding stuff to the standard library is a big thing, but I had to give it a least a try. ;)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions