Menu Close

Go – How to check if string ends with given suffix in go golang

Here, We are going to learn about checking string ends with given suffix in go golang. We can do this by using HasSuffix() function of strings package in go golang.

HasSuffix() function tests whether the string s ends with suffix.

Function Prototype:

func HasSuffix(str string, suffix string) bool

Return Value:

It returns bool value. If string str ends with given suffix, then it return ‘true’ else ‘false’.

Example with code:

package main

import (
	"fmt"
	"strings"
)

func main() {

	fmt.Println(strings.HasSuffix("TechieIndoor", "Indoor"))
	
	fmt.Println(strings.HasSuffix("TechieIndoor", "r")
	
	fmt.Println(strings.HasSuffix("TechieIndoor", "Techie"))
	
	fmt.Println(strings.HasSuffix("TechieIndoor", ""))
	
}

Output:

true
true
false
true

To learn more about golang, Please refer given below link.

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, packages, strings

Leave a Reply

Your email address will not be published. Required fields are marked *