Golang - transformation between structures

I have two structures

type A struct {
    a int
    b string
}

type B struct {
    A
    c string
    // more fields
}

I would like to convert a variable of type A to type B (A defined only the base fields, which are crucial for some parts, B, on the other hand, contains "full" data).

Is it possible in Go or do I need to manually copy the fields (or create an A.GetB () method or something like this and use this to convert A to B)?

+4
source share
2 answers

By conversion, you mean this:

func main() {
    // create structA of type A
    structA := A{a: 42, b: "foo"}

    // convert to type B
    structB := B{A: structA}
}
+6
source

Types Aand Bhave different basic types, so they cannot be converted to each other. In no case.

, , , .

-2

All Articles