In Go, you can use indexing or slicing to get the last character of a string.
In this example,
str
with a value of "Hello World"
.str
and len(str) - 1
to get the last character.package main
import "fmt"
func main() {
str := "Hello World"
lastChar := str[len(str)-1]
fmt.Printf("Last character of the string is: %c", lastChar)
}
Last character of the string is: d
In this example,
str
with a value of "apple"
.str
and len(str) - 1
to get the last character.package main
import "fmt"
func main() {
str := "apple"
lastChar := str[len(str)-1]
fmt.Printf("Last character of the string is: %c", lastChar)
}
Last character of the string is: e
In this tutorial, we learned How to get Last Character in String in Go language with well detailed examples.