Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

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

Posted on March 11, 2023March 11, 2023 by admin

Table of Contents

  • Overview
  • Code:

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

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP