How to get the last n characters of a string in Golang
In this tutorial, we will learn how to get the last n characters of a given string in Golang.
In Golang, strings are the character sequence of bytes.
Getting the last n characters
To access the last n characters of a string, we can use the slice expression []
in Go.
Here is an example, that gets the last 3 characters from the following string:
country := "France"
lastThree:= country[len(country)-3:]
fmt.Println(lastThree)
Output:
nce
In the example above, we have passed [len(country)-3:]
to the slice expression. so it starts the extraction at position len(country)-3
and extract the rest of a string.