Golang : Is conversion between different struct types possible?

前端 未结 5 833
小蘑菇
小蘑菇 2020-12-01 00:17

Let\'s say I have two similar types set this way :

type type1 []struct {
    Field1 string
    Field2 int
}
type type2 []struct {
    Field1 string
    Field         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 00:30

    You can manually use a mapper function which maps each element of type t1 to type t2. It will work.

    func GetT2FromT1(ob1 *t1) *t2 {
         ob2 := &t2 { Field1: t1.Field1, }
         return ob2
    }
    

提交回复
热议问题