Standard library / bytes

bytes.Compare()

Compares inputs lexicographically by bytes, returning -1, 0 or 1. Use bytes.Equal for equality; nil and empty slices compare equal. Expected stdout: '-1\n'.

Formal Syntax & Structure

go syntaxgo
func Compare(a, b []byte) int

Parameters & Attributes

NameTypeDescription
a[]byteFirst input to compare.
b[]byteSecond input to compare.

Return Value

int

Compares inputs lexicographically by bytes, returning -1, 0 or 1. Use bytes.Equal for equality; nil and empty slices compare equal.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt")
func main() {  fmt.Println(bytes.Compare([]byte("a"), []byte("b")))}
Go Reference