find the last index of a character in a string in go

To find the last index of a character in a string in Go, you can use the strings package and the LastIndex function. Here's an example:

main.go
package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "hello world"
	char := 'o'

	lastIndex := strings.LastIndex(str, string(char))
	fmt.Println(lastIndex)
}
171 chars
15 lines

In this example, the LastIndex function is used to find the last index of the character 'o' in the string "hello world". The function returns an integer representing the index of the last occurrence of the character in the string, which is printed to the console.

Note that the LastIndex function returns -1 if the character is not found in the string.

gistlibby LogSnag