You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"fmt""github.com/kashifkhan0771/utils/structs"
)
// User Define a struct with fields tagged as `updatable`.typeUserstruct {
IDint`updatable:"false"`// Not updatableNamestring`updatable:"true"`// updatable, uses default field nameEmailstring`updatable:"email_field"`// updatable, uses a custom tag nameAgeint`updatable:"true"`// updatable, uses default field nameIsAdminbool// Not tagged, so not updatable
}
funcmain() {
oldUser:=User{
ID: 1,
Name: "Alice",
Email: "[email protected]",
Age: 25,
IsAdmin: false,
}
newUser:=User{
ID: 1,
Name: "Alice Johnson",
Email: "[email protected]",
Age: 26,
IsAdmin: true,
}
// Compare the two struct instances.results, err:=structs.CompareStructs(oldUser, newUser)
iferr!=nil {
fmt.Println("Error:", err)
return
}
// Print the results.for_, result:=rangeresults {
fmt.Printf("Field: %s, Old Value: %v, New Value: %v\n", result.FieldName, result.OldValue, result.NewValue)
}
}
Output:
Field: Name, Old Value: Alice, New Value: Alice Johnson
Field: email_field, Old Value: [email protected], New Value: [email protected]
Field: Age, Old Value: 25, New Value: 26