Golang : Is conversion between different struct types possible?

前端 未结 5 831
小蘑菇
小蘑菇 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:25

    Nicolas, in your later comment you said you were using field tags on the struct; these count as part of definition, so t1 and t2 as defined below are different and you cannot cast t2(t1):

    type t1 struct {
        Field1 string
    }
    
    type t2 struct {
        Field1 string `json:"field_1"`
    }
    

    UPDATE: This is no longer true as of Go 1.8

提交回复
热议问题