To check if a string ends with a specific suffix in Go, you can use the strings.HasSuffix()
function.
In this example,
strings
package, which contains the HasSuffix()
function.str1
with a string value of "Hello World"
.HasSuffix()
function of the strings
package with the string object str1
and the suffix "World"
.HasSuffix()
function returns true
if the string ends with the specified suffix; otherwise, it returns false
.package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Hello World"
endsWithWorld := strings.HasSuffix(str1, "World")
fmt.Println("String ends with suffix 'World': ", endsWithWorld)
}
String ends with suffix 'World': true
In this example,
strings
package, which contains the HasSuffix()
function.str2
with a string value of "apple"
.HasSuffix()
function of the strings
package with the string object str2
and the suffix "banana"
.HasSuffix()
function returns true
if the string ends with the specified suffix; otherwise, it returns false
.package main
import (
"fmt"
"strings"
)
func main() {
str2 := "apple"
endsWithBanana := strings.HasSuffix(str2, "banana")
fmt.Println("String ends with suffix 'banana': ", endsWithBanana)
}
String ends with suffix 'banana': false
In this tutorial, we learned How to Check if String ends with Specific Suffix in Go language with well detailed examples.