How do you loop through the fields in a Golang struct to get and set values in an extensible way?

前端 未结 5 1599
孤城傲影
孤城傲影 2021-02-07 06:42

I have a struct Person.

type Person struct {
    Firstname string       
    Lastname  string       
    Years     uint8       
}

Then I have t

5条回答
  •  被撕碎了的回忆
    2021-02-07 07:37

    Reflection should be all you need. This seems similar (though not identical) to "deep copy" semantics, which has been implemented at https://godoc.org/github.com/getlantern/deepcopy

    You should be able to adapt that to your needs, or at least take some ideas from it.

提交回复
热议问题