Golang strings.HasSuffix Function

The strings.HasSuffix function in Golang is part of the strings package and is used to check whether a string ends with a specified suffix. This function is particularly useful when you need to verify if a string conforms to a certain pattern or ends with a particular substring.

Table of Contents

  1. Introduction
  2. strings.HasSuffix Function Syntax
  3. Examples
    • Basic Usage
    • Case Sensitivity
    • Working with File Extensions
  4. Real-World Use Case Example
  5. Conclusion

Introduction

The strings.HasSuffix function allows you to determine if a given string ends with a specified suffix. This can be useful in various scenarios, such as validating input, filtering file names, or processing URLs. The function performs a straightforward check and returns a boolean value indicating whether the suffix is present at the end of the string.

strings.HasSuffix Function Syntax

The syntax for the strings.HasSuffix function is as follows:

func HasSuffix(s, suffix string) bool

Parameters:

  • s: The string to be checked.
  • suffix: The suffix to be checked for at the end of the string.

Returns:

  • bool: A boolean value that is true if the string ends with the specified suffix, and false otherwise.

Examples

Basic Usage

This example demonstrates how to use strings.HasSuffix to check if a string ends with a specific suffix.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, world!"
	suffix := "world!"

	if strings.HasSuffix(str, suffix) {
		fmt.Printf("The string \"%s\" ends with \"%s\".\n", str, suffix)
	} else {
		fmt.Printf("The string \"%s\" does not end with \"%s\".\n", str, suffix)
	}
}

Output:

The string "Hello, world!" ends with "world!".

Explanation:

  • The strings.HasSuffix function checks whether the string "Hello, world!" ends with the suffix "world!".
  • Since the string ends with the specified suffix, the function returns true, and the output confirms this.

Case Sensitivity

The strings.HasSuffix function is case-sensitive, meaning that it will return false if the case of the suffix does not match the end of the string.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, World!"
	suffix := "world!"

	if strings.HasSuffix(str, suffix) {
		fmt.Printf("The string \"%s\" ends with \"%s\".\n", str, suffix)
	} else {
		fmt.Printf("The string \"%s\" does not end with \"%s\".\n", str, suffix)
	}
}

Output:

The string "Hello, World!" does not end with "world!".

Explanation:

  • The strings.HasSuffix function is case-sensitive, so it returns false when checking if "Hello, World!" ends with "world!" (lowercase "world").
  • The output confirms that the string does not end with the specified suffix due to the case mismatch.

Working with File Extensions

This example demonstrates how to use strings.HasSuffix to check for file extensions, which is a common use case.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	filename := "document.txt"
	extension := ".txt"

	if strings.HasSuffix(filename, extension) {
		fmt.Printf("The file \"%s\" has a \"%s\" extension.\n", filename, extension)
	} else {
		fmt.Printf("The file \"%s\" does not have a \"%s\" extension.\n", filename, extension)
	}
}

Output:

The file "document.txt" has a ".txt" extension.

Explanation:

  • The strings.HasSuffix function checks whether the filename "document.txt" ends with the extension ".txt".
  • Since the filename ends with the specified extension, the function returns true, and the output confirms this.

Real-World Use Case Example: URL Validation

Suppose you are building a web application that needs to validate whether certain URLs end with a specific path or file extension. You can use strings.HasSuffix to perform this validation.

Example: URL Validation

package main

import (
	"fmt"
	"strings"
)

func validateURL(url, suffix string) bool {
	return strings.HasSuffix(url, suffix)
}

func main() {
	url := "https://example.com/index.html"
	expectedSuffix := ".html"

	if validateURL(url, expectedSuffix) {
		fmt.Printf("The URL \"%s\" ends with \"%s\".\n", url, expectedSuffix)
	} else {
		fmt.Printf("The URL \"%s\" does not end with \"%s\".\n", url, expectedSuffix)
	}
}

Output:

The URL "https://example.com/index.html" ends with ".html".

Explanation:

  • The validateURL function uses strings.HasSuffix to check whether the given URL ends with the expected suffix ".html".
  • The URL validation confirms that the URL ends with the specified suffix.

Conclusion

The strings.HasSuffix function in Go is used for checking whether a string ends with a specified suffix. This function is particularly useful in scenarios such as validating input, filtering filenames, or processing URLs. By leveraging strings.HasSuffix, you can easily determine if a string conforms to a specific pattern, making it an essential function in the Go string manipulation toolkit.

Leave a Comment

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

Scroll to Top