strings.LastIndex() Function in Golang With Examples
strings.LastIndex() function in the Golang returns the starting index of the occurrence of the last instance of a substring in a given string. If the substring is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the string.
func LastIndex(str, substring string) int
Here, str is the original string and substring is a string, whose we want to find the last index value.
The string is “GeeksforGeeks” and the substring is “Geeks” so the compiler finds the substring present in the original string and displays the starting index of the last instance of substring which is 8.
21 27 -1
The string is “My favorite sport is football” and the substrings are “f”, “ll” and “SPORT” so the compiler displays the output as 21 and 27 in the first two cases respectively and since the third substring is “SPORT” which is considered as not present in the string as the function is case sensitive so it will give the result as -1.