Welcome To Golang By Example

Check if a string ends with a suffix in Go (Golang)

Table of Contents

Overview

In GO string are UTF-8 encoded. strings package of GO provides a HasSuffix method that can be used to check if a string ends with a certain suffix

Below is the signature of the function

func HasSuffix(s, prefix string) bool

Let’s look at the working code

Code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    //Input string contains the suffix
    res := strings.HasSuffix("abcdef", "ef")
    fmt.Println(res)

    //Input string doesn't contain the suffix
    res = strings.HasPrefix("abcdef", "fg")
    fmt.Println(res)
}

Output:

true
false