To check if a string ends with a specific suffix in Golang, you can use the strings.HasSuffix()
function. Here's an example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello World"
suffix := "World"
if strings.HasSuffix(str, suffix) {
fmt.Printf("The string \"%s\" ends with \"%s\"\n", str, suffix)
} else {
fmt.Printf("The string \"%s\" does not end with \"%s\"\n", str, suffix)
}
}
In the example above, we use the HasSuffix()
function from the strings
package. This function takes two parameters: the string to be checked and the suffix to be checked for. It returns a boolean value indicating whether the string ends with the specified suffix.
In the example, we check if the string "Hello World"
ends with the suffix "World"
. Since it does, the HasSuffix()
function returns true
, and we print a message confirming that the string ends with the specified suffix.