Check if the specified element is present in the slice of bytes in Golang


A slice of bytes is a dynamic array of bytes used to represent any binary data in Golang. In Golang, it is one of the most widely used data structures. The presence or absence of a particular element in a slice of bytes must often be verified. This article will cover how to check if the specified element is present in the slice of bytes in Golang.

Checking if a byte element is present in a slice of bytes

To check if the specified element is present in the slice of bytes, we can use the bytes.Contains() function from the bytes package. This function takes two arguments - the slice of bytes to search and the byte element to look for. It returns a boolean value indicating whether the byte element is present in the slice of bytes or not.

Here's an example code snippet that demonstrates how to use the bytes.Contains() function −

Example

package main

import (
   "bytes"
   "fmt"
)

func main() {
   s := []byte{'a', 'b', 'c', 'd', 'e'}
   b1 := 'a'
   b2 := 'f'

   if bytes.ContainsRune(s, b1) {
      fmt.Printf("%c is present in %v\n", b1, s)
   } else {
      fmt.Printf("%c is not present in %v\n", b1, s)
   }

   if bytes.ContainsRune(s, b2) {
      fmt.Printf("%c is present in %v\n", b2, s)
   } else {
      fmt.Printf("%c is not present in %v\n", b2, s)
   }
}

In the above code, we have a slice of bytes s and two byte elements b1 and b2. We use the bytes.Contains() function to check if b1 and b2 are present in s. The output of the above program will be −

Output

a is present in [97 98 99 100 101]
f is not present in [97 98 99 100 101]

As you can see, the function correctly detects that the byte element 'a' is present in the slice of bytes, while 'f' is not.

Checking if a byte array is present in a slice of bytes

In addition to checking for a single byte element, we can also check if a byte array is present in the slice of bytes. For this, we can use the bytes.Contains() function with two slice of bytes as its arguments.

Here's an example code snippet that demonstrates how to use the bytes.Contains() function to check if a byte array is present in the slice of bytes −

Example

package main

import (
   "bytes"
   "fmt"
)

func main() {
   s := []byte{'a', 'b', 'c', 'd', 'e'}
   b1 := []byte{'a', 'b'}
   b2 := []byte{'f', 'g'}

   if bytes.Contains(s, b1) {
      fmt.Printf("%v is present in %v\n", b1, s)
   } else {
      fmt.Printf("%v is not present in %v\n", b1, s)
   }

   if bytes.Contains(s, b2) {
      fmt.Printf("%v is present in %v\n", b2, s)
   } else {
      fmt.Printf("%v is not present in %v\n", b2, s)
   }
}

Output

[97 98] is present in [97 98 99 100 101]
[102 103] is not present in [97 98 99 100 101]

In the above code, we have a slice of bytes s and two byte arrays b1 and b2. We use the bytes.HasPrefix function to check if the slice s starts with the prefix b1. Similarly, we use the bytes.HasSuffix function to check if the slice s ends with the suffix b2. Both of these functions return a boolean value indicating whether or not the slice satisfies the specified condition.

Additionally, we use the bytes.Contains function to check if the byte slice s contains the specified element. This function returns a boolean value indicating whether or not the slice contains the specified element.

Overall, these functions make it easy to check for the presence of a prefix or suffix, or to check if an element is present in a byte slice. By using these functions, you can write more efficient and readable code for working with byte slices in Go.

Conclusion

The ability to quickly identify if an element is present in a slice or whether a slice begins or ends with a particular prefix or suffix is essential when working with slices of bytes in Go programming. In this article, we looked at a few of Go's built-in functions and methods to complete these tasks. You should be able to check for the presence of elements in slices of bytes in your Go programmes by utilising the strategies and examples covered in this article.

Updated on: 07-Apr-2023

478 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements