Go 语言程序检查两个切片是否相等


在本文中,我们将了解如何通过相关的示例来检查两个切片是否相等。切片就像数组一样,是一系列元素的序列。数组是固定元素序列,而切片是动态数组,这意味着它的值不是固定的,可以更改。切片比数组更高效、更快,此外,它们是通过引用而不是通过值传递的。让我们通过示例学习这个概念。

方法 1:使用内置函数

在这种方法中,我们将使用 reflect 包中的 reflect.DeepEqual() 函数来检查两个切片是否相等。让我们一起了解算法和代码,看看它是如何执行的。

算法

  • 步骤 1 − 创建一个 package main 并声明程序中的 fmt(格式包)包,其中 main 生成可执行代码,fmt 帮助格式化输入和输出。

  • 步骤 2 − 创建一个函数 main,并在该函数中初始化一个 myslice1,并在其中添加一些值。

  • 步骤 3 − 同样创建另外两个名为 myslice2 和 myslice3 的切片,并在这些切片中添加一些值。

  • 步骤 4 − 使用 Go 语言中的 print 语句在控制台上打印所有这些切片。

  • 步骤 5 − 使用 reflect 包中的 reflect.DeepEqual(),在第一个案例中将此函数应用于 myslice1 和 myslice2,结果将以布尔值的形式打印在控制台上,表示这两个切片是否相等。

  • 步骤 6 − 使用相同的函数与 myslice1 和 myslice3,并以相同的方式在控制台上打印输出

示例

使用内置函数检查两个切片是否相等的 Go 语言程序

package main
import (
	"fmt"
	"reflect"
)
func main() {
	myslice1 := []int{10, 20, 30}  //create slice1
	fmt.Println("The elements of slice1 are:", myslice1)
	myslice2 := []int{10, 20, 30}   //create slice2 
	fmt.Println("The elements of slice2 are:", myslice2)

	myslice3 := []int{40, 50, 60}  //create slice3
	fmt.Println("The elements of slice3 are:", myslice3)

	fmt.Println("Let's check whether the slices are equal or not")
	fmt.Println("Are the slice1 and slice2 equal?")

	fmt.Println(reflect.DeepEqual(myslice1, myslice2)) // true
	fmt.Println("Are the slice1 and slice3 equal?")
	fmt.Println(reflect.DeepEqual(myslice1, myslice3)) // false
}

输出

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [10 20 30]
The elements of slice3 are: [40 50 60]
Let's check whether the slices are equal or not
Are the slice1 and slice2 equal?
true
Are the slice1 and slice3 equal?
false

方法 2:遍历切片元素

在这种方法中,我们将通过遍历切片的元素来查看两个切片是否相等。我们将比较切片的元素并找到结果。让我们一起了解算法和代码,看看它是如何执行的。

算法

  • 步骤 1 − 创建一个 package main 并声明程序中的 fmt(格式包)包,其中 main 生成可执行代码,fmt 帮助格式化输入和输出。

  • 步骤 2 − 创建一个函数 main,并在该函数中初始化一个 myslice1,并在其中添加一些值。

  • 步骤 3 − 同样创建另一个名为 myslice2 的切片,并在该切片中添加一些值。

  • 步骤 4 − 使用 Go 语言中的 print 语句在控制台上打印所有这些切片。

  • 步骤 5 − 创建一个名为 slice_equality 的函数,其参数为 myslice1 和 myslice2,函数返回的值类型为布尔型。

  • 步骤 6 − 检查条件:如果 myslice1 和 myslice2 的长度不相等,则向函数返回 false。

  • 步骤 7 − 运行一个循环,直到 myslice1 的长度,并比较两个切片的元素。

  • 步骤 8 − 如果两个切片的元素不相等,则返回 false,但如果它们相等,则向函数返回 true。

  • 步骤 9 − 输出将使用 fmt.Println() 函数打印,其中 ln 表示换行。

示例

通过遍历切片元素检查两个切片是否相等的 Go 语言程序。

package main
import (
	"fmt"
)
func main() {
	myslice1 := []int{10, 20, 30} //create slice1
	fmt.Println("The elements of slice1 are:", myslice1)
	myslice2 := []int{10, 20, 30} //create slice2
	fmt.Println("The elements of slice2 are:", myslice2)

	fmt.Println("Let's check whether the slices are equal or not")
	fmt.Println("Are the slice1 and slice2 equal?")

	fmt.Println(slice_equality(myslice1, myslice2)) // true

}

func slice_equality(myslice1, myslice2 []int) bool {
	if len(myslice1) != len(myslice2) {  //if condition is not satisfied print false
		return false
	}
	for i, element := range myslice1 {  // use for loop to check equality
		if element != myslice2[i] {
			return false
		}
	}
	return true
}

输出

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [10 20 30]
Let's check whether the slices are equal or not
Are the slice1 and slice2 equal?
true

结论

在上面的程序中,我们使用了两个示例来检查两个切片是否相等。在第一种方法中,我们使用了 reflect 包的函数,在第二种方法中,我们使用了 for 循环来比较切片并检查它们是否相等。

更新于: 2023 年 1 月 23 日

1K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.