I have structs like below:
type Foo struct{
A string
B string
}
type Bar struct{
C string
D Baz
}
type Baz struct{
E string
F string
}
Lets say I have []Bar
, how to convert this to []Foo
?
A
should be C
B
should be E
I have structs like below:
type Foo struct{
A string
B string
}
type Bar struct{
C string
D Baz
}
type Baz struct{
E string
F string
}
Lets say I have []Bar
, how to convert this to []Foo
?
A
should be C
B
should be E
I don't think there's any "magic" way of doing the conversion. However, it is a very small piece of coding to create it. Something like this ought to do the trick.
func BarsToFoos(bs []Bar) []Foo {
var acc []Foo
for _, b := range bs {
newFoo := Foo{A: b.C, B: b.D.E} // pulled out for clarity
acc = append(acc, newFoo)
}
return acc
}