看一个文章有段代码有误,改了下,测试通过

package main

import (
    "fmt"
    "github.com/google/go-cmp/cmp"
    "reflect"
)

type Contact struct {
    Phone string
    Email string
}

type User struct {
    Name    string
    Age     int
    contact Contact
}

func allowUnExportedInType(t reflect.Type) bool {
    if t.Name() == "User" {
       return true
    }

    return false
}

func main() {
    c1 := Contact{Phone: "123456789", Email: "[email protected]"}
    c2 := Contact{Phone: "123456789", Email: "[email protected]"}

    u1 := User{"dj", 18, c1}
    u2 := User{"dj", 18, c2}

    fmt.Println("u1 equals u2?", cmp.Equal(u1, u2, cmp.Exporter(allowUnExportedInType)))
}

来源地址:

Go 每日一库之 go-cmp - 知乎, 其中的 Exporter 小节。

你可能感兴趣的:(Golang,golang)