I'm new to Go and have this problem to which I can't find a solution (I don't even know if it is a problem or if it is the expected behavior).
I have a Struct like the following:
type Info struct {
Id string
Request interface{}
}
Request can be of different types, ex.
type ReqType1 struct {
Quantity int
Code float32
}
or
type ReqType2 struct {
Name string
Code float32
}
My question is: if I have var i
of type Info
, and i.Request
type is ReqType2
, is it possible to set the value of Name
without needing another variable request
(like the following)?
I know I can just do:
request := i.Request.(ReqType2)
request.Name = "Maddie"
i.Request = request
But I have a lot more attributes, and was hoping there was another way to do so. I tried using reflect pkg but couldn't get it right. I tried:
reflect.ValueOf(i.Request).Elem().FieldByName("Name").SetString("Maddie")
Aucun commentaire:
Enregistrer un commentaire