"Go ElementsEqual()"의 두 판 사이의 차이

35번째 줄: 35번째 줄:
==같이 보기==
==같이 보기==
* [[Go DeepEqual()]]
* [[Go DeepEqual()]]
* [[Go isSuperSlice()]]
* [[함수 ElementsEqual()]]
* [[함수 ElementsEqual()]]



2022년 7월 29일 (금) 16:48 판

1 개요

Go ElementsEqual()
package main

import (
	"fmt"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

func main() {
	a := []int{1, 2, 11, 12}
	b := []int{12, 2, 1, 11}
	fmt.Println(intElementsEqual(a, b)) // true

	x := []string{"a", "b", "c"}
	y := []string{"a", "c", "b"}
	fmt.Println(stringElementsEqual(x, y)) // true

}

func intElementsEqual(a, b []int) bool {
	return cmp.Diff(a, b, cmpopts.SortSlices(func(x, y int) bool { return x < y })) == ""
}

func stringElementsEqual(a, b []string) bool {
	return cmp.Diff(a, b, cmpopts.SortSlices(func(x, y string) bool { return x < y })) == ""
}


2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}