Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "reflect"
- )
- type User struct {
- Username string
- Password string
- }
- type Person struct {
- Username string
- Password string
- }
- func main() {
- p1 := &User{Username: "rodrigo", Password: "pass"}
- p2 := &Person{Username: "asdasd", Password: "pass"}
- fmt.Println(EqualStruct(p1, p2))
- }
- func EqualStruct(x, y interface{}) bool {
- if x == nil || y == nil {
- return x == y
- }
- v1 := reflect.Indirect(reflect.ValueOf(x))
- v2 := reflect.Indirect(reflect.ValueOf(y))
- if v1.NumField() != v2.NumField() {
- return false
- }
- for i := 0; i < v1.NumField(); i++ {
- if v1.Type().Field(i).Name != v1.Type().Field(i).Name {
- return false
- }
- }
- return true
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement