Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
How to check equality of slices of bytes in Golang?
Next article icon

How to check equality of slices of bytes in Golang?

Last Updated : 02 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In Go language slice is more powerful, flexible, and convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence that stores elements of a similar type, you are not allowed to store different types of elements in the same slice. In the Go slice of byes, you are allowed to check the equality of the slices with the help of Equal() function. This function returns true if both the slices are equal, or returns false if both the slices are not equal. It is defined under the bytes package so, you have to import bytes package in your program for accessing Equals function. 

Syntax:

func Equal(slice_1, slice_1 []byte) bool

In Go, you can check the equality of slices of bytes using the built-in bytes.Equal function from the bytes package. The bytes.Equal function takes two arguments, both of type []byte, and returns a boolean indicating whether the two slices are equal or not.

Here's an example that demonstrates how to check the equality of two slices of bytes in Go:

Go
package main  import (     "bytes"     "fmt" )  func main() {     slice1 := []byte{1, 2, 3, 4, 5}     slice2 := []byte{1, 2, 3, 4, 5}     slice3 := []byte{5, 4, 3, 2, 1}      fmt.Println(bytes.Equal(slice1, slice2)) // true     fmt.Println(bytes.Equal(slice1, slice3)) // false } 

Output:

true false

In this example, the two slices slice1 and slice2 are equal, and the bytes.Equal function returns true. The two slices slice1 and slice3 are not equal, and the bytes.Equal function returns false.

The bytes.Equal function is a simple and efficient way to check the equality of two slices of bytes in Go, and it's widely used in Go programs for this purpose.

Here, we check the equality between slice_1 and slice_2 and the return type of this function is bool. Let us discuss this concept with the help of the examples:

Example 1: 

Go
// Go program to illustrate how to // check the equality of the slices  package main  import (     "bytes"     "fmt" )  func main() {      // Creating and initializing slices of bytes     // Using shorthand declaration      slice_1 := []byte{'A', 'N', 'M', 'A',                     'P', 'A', 'A', 'W'}          slice_2 := []byte{'A', 'N', 'M', 'A',                     'P', 'A', 'A', 'W'}      // Checking the equality of the slices     // Using Equal function     res := bytes.Equal(slice_1, slice_2)          if res == true {              fmt.Println("Slice_1 is equal to Slice_2")     } else {              fmt.Println("Slice_1 is not equal to Slice_2")     }  } 

Output:

Slice_1 is equal to Slice_2

Example 2: 

Go
// Go program to illustrate how to // check the equality of the slices  package main  import (     "bytes"     "fmt" )  func main() {      // Creating and initializing     // slices of bytes     // Using shorthand declaration     slice_1 := []byte{'A', 'N', 'M',             'A', 'P', 'A', 'A', 'W'}          slice_2 := []byte{'g', 'e', 'e', 'k', 's'}          slice_3 := []byte{'A', 'N', 'M', 'A',                     'P', 'A', 'A', 'W'}      // Checking the equality of the slices     // Using Equal function     res1 := bytes.Equal(slice_1, slice_2)     res2 := bytes.Equal(slice_1, slice_3)     res3 := bytes.Equal(slice_2, slice_3)     res4 := bytes.Equal([]byte("GeeksforGeeks"),                         []byte("GeeksforGeeks"))     res5 := bytes.Equal([]byte("Geeks"), []byte("GFG"))     res6 := bytes.Equal(slice_1, []byte("P"))      // Displaying results     fmt.Println("Result 1:", res1)     fmt.Println("Result 2:", res2)     fmt.Println("Result 3:", res3)     fmt.Println("Result 4:", res4)     fmt.Println("Result 5:", res5)     fmt.Println("Result 6:", res6)  } 

Output:

Result 1: false Result 2: true Result 3: false Result 4: true Result 5: false Result 6: false

Next Article
How to check equality of slices of bytes in Golang?

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang
  • Golang-Slices

Similar Reads

    How to Compare Equality of Struct, Slice and Map in Golang?
    In Golang, reflect.DeepEqual function is used to compare the equality of struct, slice, and map in Golang. It is used to check if two elements are "deeply equal" or not. Deep means that we are comparing the contents of the objects recursively. Two distinct types of values are never deeply equal. Two
    3 min read
    How to compare two slices of bytes in Golang?
    In Go, a slice is a powerful, flexible data structure that allows elements of the same type to be stored in a variable-length sequence. When working with byte slices ([]byte), Go provides easy-to-use functions in the bytes package to compare them. In this article we will learn "How to compare two sl
    2 min read
    How to split a slice of bytes in Golang?
    In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
    3 min read
    How to trim a slice of bytes in Golang?
    In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you ar
    3 min read
    How to sort a slice of ints in Golang?
    In Go, slices provide a flexible way to manage sequences of elements. To sort a slice of ints, the sort package offers a few straightforward functions. In this article we will learn How to Sort a Slice of Ints in Golang.ExampleGopackage main import ( "fmt" "sort" ) func main() { intSlice := []int{42
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences