The Wayback Machine - https://web.archive.org/web/20240210022609/https://www.geeksforgeeks.org/check-if-the-string-ends-with-specified-suffix-in-golang/
Open In App
Related Articles

Check if the String ends with specified suffix in Golang

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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:
// Go program to illustrate how to check the
// given string start with the specified prefix
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing strings
    // Using shorthand declaration
    s1 := "I am working as a Technical content writer in GeeksforGeeks!"
    s2 := "I am currently writing articles on Go language!"
  
    // Checking the given strings 
    // starts with the specified prefix
    // Using HasSuffix() function
    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")
  
    // Displaying results
    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
Previous
Next
Share your thoughts in the comments
Similar Reads