get the last char in a string in go

To get the last character of a string in Go, you can use indexing.

Here's an example:

main.go
package main

import "fmt"

func main() {
    str := "hello"
    lastChar := str[len(str)-1]
    fmt.Println(string(lastChar)) // Output: o
}
142 chars
10 lines

We retrieve the last character of the string str by using indexing with len(str)-1. The string() function is then used to convert the integer representation of the last character back into a string, which is then printed to the console with fmt.Println().

gistlibby LogSnag