用另一个结构体赋值struct [英] Assign struct with another struct
问题描述
type RegistrationRequest struct {
Email * string
Email2 *字符串
用户名*字符串
密码*字符串
名称字符串
}
其中电子邮件2
是再次输入的电子邮件值,以验证用户输入的内容是否正确。
我也有一个用户结构:
类型用户结构{
电子邮件*字符串
用户名*字符串
密码*字符串
名称字符串
}
当然,没有必要存储超过注册的 Email2
。
req
和 u
- 每个结构体一个。是否可以将 req
结构分配给 u
结构,这样所有的公共字段将存在于 u
struct?使用简单的赋值,因为即使 User
的字段是 RegistrationRequest
,它们完全是两种不同的类型,而赋值规则不适用。
您可以编写一个使用反射的函数( reflect
包),并将所有字段从 req
复制到 u
,但这只是丑陋的(而且效率低下)。
最好的办法是重构你的类型, code> RegistrationRequest 可以 embed User
。
如果您的类型值 RegistrationRequest
,这意味着您已经具有 User
的值:
类型User struct {
电子邮件*字符串
用户名*字符串
密码*字符串
名称字符串
}
type RegistrationRequest struct {
User //嵌入用户类型
Email2 * string
}
func main(){
req:= RegistrationRequest {}
s:=as@as.com
req.Email =& s
s2:=testuser
req.Username =& s2
$ b $:$ User $ {
u} / code>
输出:(在
) c $ c> testuser as@as.com
$ rel =noreferrer> Go Playground
另外请注意,由于您的structs co ntain指针,当复制 struct
时,指针值将被复制,而不是指向值。我不确定为什么你需要指针,最好只声明所有的字段是非指针。
另外请注意,嵌入并不是真正的要求,它只是让你的类型和使用更加流畅。 用户
也可以是 RequisitionRequest
的普通字段,例如:
type RegistrationRequest struct {
Usr User //这只是一个普通的字段,不包含
Email2 * string
}
I have a RegistrationRequest struct:
type RegistrationRequest struct {
Email *string
Email2 *string
Username *string
Password *string
Name string
}
Where Email2
is the email value entered again to verify that what the user entered is correct.
I also have a User struct:
type User struct {
Email *string
Username *string
Password *string
Name string
}
Of course, there is no need to store Email2
beyond registration.
So I have two variables: req
and u
- one for each struct. Is it possible to assign the req
struct into to the u
struct so that all the common fields will exist in the u
struct?
Using simple assignment you can't because even though the fields of User
are a subset of RegistrationRequest
, they are completely 2 different types, and Assignability rules don't apply.
You could write a function which uses reflection (reflect
package), and would copy all the fields from req
to u
, but that is just ugly (and inefficient).
Best would be to refactor your types, and RegistrationRequest
could embed User
.
Doing so if you have a value of type RegistrationRequest
that means you already also have a value of User
:
type User struct {
Email *string
Username *string
Password *string
Name string
}
type RegistrationRequest struct {
User // Embedding User type
Email2 *string
}
func main() {
req := RegistrationRequest{}
s := "as@as.com"
req.Email = &s
s2 := "testuser"
req.Username = &s2
u := User{}
u = req.User
fmt.Println(*u.Username, *u.Email)
}
Output: (try it on the Go Playground)
testuser as@as.com
Also please note that since your structs contain pointers, when copying a struct
, pointer values will be copied and not pointed values. I'm not sure why you need pointers here, would be best to just declare all fields to be non-pointers.
Also note that embedding is not really a requirement, it just makes your types and their usage more smooth. User
could just as well be an "ordinary" field of RequistrationRequest
, e.g.:
type RegistrationRequest struct {
Usr User // This is just an ordinary field, not embedding
Email2 *string
}
这篇关于用另一个结构体赋值struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!