# 如何在Golang中比较两个字节切片？

• 如果结果为0, 则slice_1 == slice_2。
• 如果结果为-1, 则slice_1 <slice_2。
• 如果结果为+1, 则slice_1> slice_2。

``func Compare(slice_1, slice_2 []byte) int``

``````// Go program to illustrate how to
// compare two slices of bytes
package main

import (
"bytes"
"fmt"
)

// Main function
func main() {

// Creating and initializing
// slices of bytes
// Using shorthand declaration

slice_1 := []byte{ 'G' , 'E' , 'E' , 'K' , 'S' }
slice_2 := []byte{ 'G' , 'E' , 'e' , 'K' , 'S' }

// Comparing slice
// Using Compare function
res := bytes.Compare(slice_1, slice_2)

if res == 0 {
fmt.Println( "!..Slices are equal..!" )
} else {
fmt.Println( "!..Slice are not equal..!" )
}
}``````

``!..Slice are not equal..!``

``````// Go program to illustrate how
// to compare two slices of byte
package main

import (
"bytes"
"fmt"
)

func main() {

// Creating and initializing
// slices of bytes
// Using shorthand declaration
slice_1 := []byte{ 'A' , 'N' , 'M' , 'O' , 'P' , 'Q' }
slice_2 := []byte{ 'a' , 'g' , 't' , 'e' , 'q' , 'm' }
slice_3 := []byte{ 'A' , 'N' , 'M' , 'O' , 'P' , 'Q' }
slice_4 := []byte{ 'A' , 'n' , 'M' , 'o' , 'p' , 'Q' }

// Displaying slices
fmt.Println( "Slice 1: " , slice_1)
fmt.Println( "Slice 2: " , slice_2)
fmt.Println( "Slice 3: " , slice_3)
fmt.Println( "Slice 4: " , slice_4)

// Comparing slices
// Using Compare function
res1 := bytes.Compare(slice_1, slice_2)
res2 := bytes.Compare(slice_1, slice_3)
res3 := bytes.Compare(slice_1, slice_4)
res4 := bytes.Compare(slice_2, slice_3)
res5 := bytes.Compare(slice_2, slice_4)
res6 := bytes.Compare(slice_2, slice_1)
res7 := bytes.Compare(slice_3, slice_1)
res8 := bytes.Compare(slice_3, slice_2)
res9 := bytes.Compare(slice_3, slice_4)
res10 := bytes.Compare(slice_4, slice_1)
res11 := bytes.Compare(slice_4, slice_2)
res12 := bytes.Compare(slice_4, slice_3)
res13 := bytes.Compare(slice_4, slice_4)

// Displaying results
fmt.Println( "\nResult 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)
fmt.Println( "Result 7:" , res7)
fmt.Println( "Result 8:" , res8)
fmt.Println( "Result 9:" , res9)
fmt.Println( "Result 10:" , res10)
fmt.Println( "Result 11:" , res11)
fmt.Println( "Result 12:" , res12)
fmt.Println( "Result 13:" , res13)
}``````

``````Slice 1:  [65 78 77 79 80 81]
Slice 2:  [97 103 116 101 113 109]
Slice 3:  [65 78 77 79 80 81]
Slice 4:  [65 110 77 111 112 81]

Result 1: -1
Result 2: 0
Result 3: -1
Result 4: 1
Result 5: 1
Result 6: 1
Result 7: 0
Result 8: -1
Result 9: -1
Result 10: 1
Result 11: -1
Result 12: 1
Result 13: 0``````

• 回顶