feat(Go-StudyExample):2020/12/31:新增【Go每日一库】go-cmp结构体比较工具

This commit is contained in:
Huangzj
2020-12-31 14:48:58 +08:00
parent 20c2c7fab0
commit 6d079549f1
10 changed files with 465 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
/*
* @Author : huangzj
* @Time : 2020/12/30 14:49
* @Description:自定义Equal方法...
*/
package go_cmp
import (
"fmt"
"github.com/google/go-cmp/cmp"
"testing"
)
type NetAddr struct {
IP string
Port int
}
func (a NetAddr) Equal(b NetAddr) bool {
if a.Port != b.Port {
return false
}
if a.IP != b.IP {
if a.IP == "127.0.0.1" && b.IP == "localhost" {
return true
}
if a.IP == "localhost" && b.IP == "127.0.0.1" {
return true
}
return false
}
return true
}
func TestDiyEqualMethod(t *testing.T) {
a1 := NetAddr{"127.0.0.1", 5000}
a2 := NetAddr{"localhost", 5000}
a3 := NetAddr{"192.168.1.1", 5000}
fmt.Println("a1 equals a2?", cmp.Equal(a1, a2))
fmt.Println("a1 equals a3?", cmp.Equal(a1, a3))
}