find the first index of a substring in a string in go

In Go, you can find the index of the first occurrence of a substring in a string using the strings.Index() function. Here's an example:

main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello, World!"
    sub := "llo"
    index := strings.Index(str, sub)

    fmt.Println(index)
}
171 chars
15 lines

In this example, strings.Index() returns the index of the first occurrence of "llo" in "Hello, World!", which is 2.

Note that strings.Index() returns -1 if the substring is not found in the string.

gistlibby LogSnag