Skip to content
Related Articles

Related Articles

Check if the String ends with specified suffix in Golang

View Discussion
Improve Article
Save Article
  • Last Updated : 26 Aug, 2019
View Discussion
Improve Article
Save Article

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.


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.


// Go program to illustrate how to check the
// given string start with the specified prefix
package main
import (
// 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)


Result 1:  true
Result 2:  true
Result 3:  false
Result 4:  true
Result 5:  false
Result 6:  true
Result 7:  false

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!