Golang 教程
Golang 控制语句
Golang 函数 & 方法
Golang 切片 & 数组
Golang 结构体
Golang 字符串
Golang 接口
Golang 指针
Golang 并发
Golang 异常(Error)
Golang 其他杂项

Go 语言切片比较

时间:2023年06月10日 阅读:188
以下内容仅是站长或网友个人学习笔记、总结和研究收藏。不保证正确性,因使用而带来的风险与本站无关!
淘客轩-衣食住行外卖生活好助手

在Go语言中,切片比数组更强大,灵活,方便,并且是轻量级的数据结构。切片是可变长度的序列,用于存储相同类型的元素,不允许在同一切片中存储不同类型的元素。在Go切片中,可以使用Compare()函数将两个字节类型的切片彼此进行比较。此函数返回一个整数值,该整数值表示这些切片相等或不相等,并且这些值是:

  • 如果结果为0,则slice_1 == slice_2。

  • 如果结果为-1,则slice_1 <slice_2。

  • 如果结果为+1,则slice_1> slice_2。

该函数在bytes包下定义,因此,您必须在程序中导入bytes包才能访问Compare函数。

语法:

func Compare(slice_1, slice_2 []byte) int

让我们借助示例来讨论这个概念:

//比较两个字节切片
package main

import (
    "bytes"
    "fmt"
)

func main() {

    //使用简写声明创建和初始化字节片

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

    //比较切片

    //使用Compare函数
    res := bytes.Compare(slice_1, slice_2)

    if res == 0 {
        fmt.Println("!..切片相等..!")
    } else {
        fmt.Println("!..切片不相等..!")
    }
}

输出:

!..切片不相等..!

比较两个字节的片示例:

package main

import (
    "bytes"
    "fmt"
)

func main() {

    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'}

    //显示切片
    fmt.Println("切片 1: ", slice_1)
    fmt.Println("切片 2: ", slice_2)
    fmt.Println("切片 3: ", slice_3)
    fmt.Println("切片 4: ", slice_4)

    //比较切片,使用Compare
    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)

    fmt.Println("\n结果 1:", res1)
    fmt.Println("结果 2:", res2)
    fmt.Println("结果 3:", res3)
    fmt.Println("结果 4:", res4)
    fmt.Println("结果 5:", res5)
    fmt.Println("结果 6:", res6)
    fmt.Println("结果 7:", res7)
    fmt.Println("结果 8:", res8)
    fmt.Println("结果 9:", res9)
    fmt.Println("结果 10:", res10)
    fmt.Println("结果 11:", res11)
    fmt.Println("结果 12:", res12)
    fmt.Println("结果 13:", res13)
}

输出:

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

结果 1: -1
结果 2: 0
结果 3: -1
结果 4: 1
结果 5: 1
结果 6: 1
结果 7: 0
结果 8: -1
结果 9: -1
结果 10: 1
结果 11: -1
结果 12: 1
结果 13: 0
打赏

本文地址:https://www.momojc.cn/golang/go-compare-two-slices-of-bytes.html

关于本站 | 隐私政策 | 免责声明 | 广告合作 | 我要投稿 | 后台管理

CopyRight © 2023-2024 MOMO教程 WWW.MOMOJC.CN , All Rights Reserved.

站长E-mail:378074730@qq.com 网站已运行:  运行时长:0.040 秒

京ICP备20029690号-1京ICP备20029690号-2 京公网安备11011402013892号京公网安备11011402013892号 中国互联网违法和不良信息举报中心 网络违法犯罪举报网站

本网站托管于 腾讯云 .由网站卫士提供网站加速和攻击防御服务 提供CDN加速/防御服务.由zblogcn强力驱动 又拍云提供CDN加速/云存储服务 51la网站统计