Go HasSuffix()
Published Sep 1, 2023
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 mainimport ("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:
truefalse
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.
Learn Go on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use Go (Golang), an open-source programming language supported by Google!
- Beginner Friendly.6 hours