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 compare two slices of bytes in Golang?
Next article icon

How to compare two slices of bytes in Golang?

Last Updated : 06 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 slices of bytes in Golang".

Example

slice1 := []byte{'G', 'o', 'l', 'a', 'n', 'g'} slice2 := []byte{'G', 'o', 'l', 'a', 'n', 'g'} slice3 := []byte{'g', 'o', 'L', 'A', 'N', 'g'}

Syntax

func Compare(a, b []byte) int                                     #bytes.Compare() func Equal(a, b []byte) bool                                         #bytes.Equal() func DeepEqual(x, y interface{}) bool                      #reflect.DeepEqual()

Table of Content

  • Comparing with bytes.Compare()
  • Comparing with bytes.Equal()
  • Using reflect.DeepEqual() (Alternative Method)

Comparing with bytes.Compare()

The bytes.Compare function returns:

  • 0 if the slices are equal,
  • -1 if slice1 is less than slice2, and
  • 1 if slice1 is greater than slice2.

Syntax

func Compare(a, b []byte) int

Example:

Go
package main import (     "bytes"     "fmt" )  func main() {     slice1 := []byte{'G', 'o', 'l', 'a', 'n', 'g'}     slice2 := []byte{'G', 'o', 'l', 'a', 'n', 'g'}     slice3 := []byte{'g', 'o', 'L', 'A', 'N', 'g'}      fmt.Println(bytes.Compare(slice1, slice2)) // Output: 0 (equal)     fmt.Println(bytes.Compare(slice1, slice3)) // Output: -1 (slice1 < slice3) } 

Output
0 -1 

Comparing with bytes.Equal()

To directly check if two byte slices are equal, use bytes.Equal, which returns true if the slices are identical.

Syntax

func Equal(a, b []byte) bool

Example

Go
package main import (     "bytes"     "fmt" ) func main() {     slc1 := []byte{'G', 'o', 'l', 'a', 'n', 'g'}     slc2 := []byte{'G', 'o', 'l', 'a', 'n', 'g'}     slc3 := []byte{'g', 'o', 'L', 'A', 'N', 'g'}      fmt.Println(bytes.Equal(slc1, slc2)) // Output: true     fmt.Println(bytes.Equal(slc1, slc3)) // Output: false } 

Output
true false 

Using reflect.DeepEqual() (Alternative Method)

reflect.DeepEqual compares the values and structure of two variables. While not specific to byte slices, it can also be used for equality checks.

Syntax

func DeepEqual(x, y interface{}) bool

Example

Go
package main import (     "fmt"     "reflect" ) func main() {     slc1 := []byte{'G', 'o', 'l', 'a', 'n', 'g'}     slc2 := []byte{'G', 'o', 'l', 'a', 'n', 'g'}     slc3 := []byte{'g', 'o', 'L', 'A', 'N', 'g'}      fmt.Println(reflect.DeepEqual(slc1, slc2)) // Output: true     fmt.Println(reflect.DeepEqual(slc1, slc3)) // Output: false } 

Output
true false 

Next Article
How to compare two slices of bytes in Golang?

A

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

Similar Reads

    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 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 check equality of slices of bytes in Golang?
    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
    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
    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
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