I've been reading about type alias and composed structs in Golang. I want to be able to have two structs which are structurally identical but can be easily converted between each other.
I have a parent structure defined as:
type User struct {
Email string `json:"email"`
Password string `json:"password"`
}
And a composed struct defined as:
type PublicUser struct {
*User
}
I would expect that if I define a User
:
a := User{
Email: "admin@example.net",
Password: "1234",
}
I could then perform the following type conversion:
b := (a).(PublicUser)
But it fails with an invalid type assertion:
invalid type assertion: a.(PublicUser) (non-interface type User on left)
How can I convert between structurally similar types in Go?