Check if the Slice of bytes ends with specified suffix in Golang


Checking whether a slice of bytes ends using a specified suffix is a typical task in Golang. Golang's bytes package includes a function called HasSuffix that determines whether or not a given byte slice ends with the specified suffix. In this article, we will discuss the syntax and usage of the HasSuffix function and provide some examples.

Syntax of HasSuffix Function

The HasSuffix function is a part of the bytes package in Golang, and it takes two arguments. The first argument is the byte slice that needs to be checked, and the second argument is the suffix that needs to be checked against the byte slice.

The syntax of the HasSuffix function is as follows −

func HasSuffix(s, suffix []byte) bool

In the above syntax, s is the byte slice that needs to be checked, and suffix is the suffix that needs to be checked against the byte slice. The function returns a boolean value of true if the byte slice ends with the specified suffix, otherwise false.

Examples

Let's look at some examples to understand how to use the HasSuffix function in Golang.

Example 1: Check if a byte slice ends with a suffix

package main

import (
   "fmt"
   "bytes"
)

func main() {
   slice1 := []byte("hello, world!")
   suffix1 := []byte("world!")
   result1 := bytes.HasSuffix(slice1, suffix1)
   fmt.Println(result1)

   slice2 := []byte("this is a test")
   suffix2 := []byte("abc")
   result2 := bytes.HasSuffix(slice2, suffix2)
   fmt.Println(result2)
}

Output

true
false

In the above example, we have two byte slices. We are checking if the first slice ends with the suffix "world!" and the second slice ends with the suffix "abc". The first slice ends with the suffix "world!", so the HasSuffix function returns true, whereas the second slice does not end with the suffix "abc", so the function returns false.

Example 2: Check if a byte slice ends with a suffix using if-else statement

package main

import (
   "fmt"
   "bytes"
)

func main() {
   slice1 := []byte("example.pdf")
   suffix1 := []byte(".pdf")
   if bytes.HasSuffix(slice1, suffix1) {
      fmt.Println("This is a PDF file")
   } else {
      fmt.Println("This is not a PDF file")
   }

   slice2 := []byte("document.txt")
   suffix2 := []byte(".pdf")
   if bytes.HasSuffix(slice2, suffix2) {
      fmt.Println("This is a PDF file")
   } else {
      fmt.Println("This is not a PDF file")
   }
}

Output

This is a PDF file
This is not a PDF file

In the above example, we have two byte slices. We are checking if the first slice ends with the suffix ".pdf" using an if-else statement. If the slice ends with the suffix ".pdf", we print "This is a PDF file", otherwise we print "This is not a PDF file". Similarly, we are checking if the second slice ends with the suffix ".pdf".

Conclusion

It is simple to determine in Golang whether a slice of bytes ends with a given suffix by utilising the HasSuffix function offered by the bytes package. We hope that this article has provided you with understanding of the HasSuffix function's functionality and applications.

Updated on: 07-Apr-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements