HasSuffix()

Published Sep 1, 2023
Contribute to Docs

The HasSuffix() function in Go is used to check if a specified suffix exists on a given string. The function returns true if the given string has the specified suffix, otherwise it returns false.

Syntax

strings.HasSuffix(str, suffix)

Where str is the given string and suffix is the ending substring to check for. This function is case-sensitive.

Example

The following example demonstrates the use of the strings.HasSuffix() function.

package main
import (
"fmt"
"strings"
)
func main() {
str := "Codecademy"
firstSuffix := "demy"
secondSuffix := "academy"
fmt.Println(strings.HasSuffix(str, firstSuffix))
fmt.Println(strings.HasSuffix(str, secondSuffix))
}

The output will be:

true
false

Codebyte Example

The following example is runnable and uses the strings.HasSuffix() function to check if the given suffixes are present in the string Hello World.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy