dimanche 11 juin 2023

How to assign a interface{} to another interface{} whose concrete type is SameStructType?

type SameStructType struct{
    Test string
}

func A() interface{} {
    return SameStructType{
        Test:"123",
    }
}
func B() interface{} {
    return &SameStructType{}
}

a:=A()
b:=B()

// *b=a //I want to make b assigned by a.

If I know the actual type, I can use the code below:

a := A().(SameStructType)
b := B().(*SameStructType)
*b = a

The question is I don't know their actual type,but I know they have the same type.

Is there an easy way instead of using "reflect"? I don't like parsing the struct "field by field" using reflect because it is slow.

If I have to use reflect,is there an easy way? I am sure that they have the same type,so is there a fast way to copy memory like C/C++?





Aucun commentaire:

Enregistrer un commentaire