string find index in golang

The strings package in Go provides a function called Index, which can be used to find the index of a substring within a given string. The syntax for using the Index function is as follows:

func Index(s, substr string) int

Here, the s parameter represents the input string in which we want to search for the substring, and the substr parameter represents the substring that we want to find within the input string. The function returns the index of the first occurrence of the substring within the input string. If the substring is not found, the function returns -1.

To use the Index function, you can simply call it with the input string and the substring as arguments, like this:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "Hello, World!"
    substr := "World"
    index := strings.Index(str, substr)
    fmt.Println(index)
}

In this example, the Index function is used to find the index of the substring "World" within the input string "Hello, World!". The value of index will be 7, as "World" starts at index 7 within the input string.