In Go language,
strings are different from other languages like
Java ,
C++ ,
Python , etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
In Go strings, you can check whether the string ends with the specified suffix or not with the help of
HasSuffix() function. This function returns
true if the given string ends with the specified suffix and return false if the given string does not end with the specified suffix. It is defined under the strings package so, you have to import strings package in your program for accessing
HasSuffix function.
Syntax:
func HasSuffix(str, suf string) bool
Here,
str is the original string and
suf is a string which represents the suffix. The return type of this function is of the bool type.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
s1 := "I am working as a Technical content writer in GeeksforGeeks!"
s2 := "I am currently writing articles on Go language!"
res1 := strings.HasSuffix(s1, "GeeksforGeeks!")
res2 := strings.HasSuffix(s1, "!")
res3 := strings.HasSuffix(s1, "Apple")
res4 := strings.HasSuffix(s2, "language!")
res5 := strings.HasSuffix(s2, "dog")
res6 := strings.HasSuffix("GeeksforGeeks, Geeks", "Geeks")
res7 := strings.HasSuffix("Welcome to GeeksforGeeks", "Welcome")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
fmt.Println("Result 4: ", res4)
fmt.Println("Result 5: ", res5)
fmt.Println("Result 6: ", res6)
fmt.Println("Result 7: ", res7)
}
Output:
Result 1: true
Result 2: true
Result 3: false
Result 4: true
Result 5: false
Result 6: true
Result 7: false
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
26 Aug, 2019
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...