Is it possible to copy the interface as well as the structure?
type MyInterface interface{} myInterface2 := *myInterface Now invalid indirect of error
Is it possible to copy the interface as well as the structure?
type MyInterface interface{} myInterface2 := *myInterface Now invalid indirect of error
With myInterface2 := *myInterface compiler does not know what it means to dereference (*) an interface, since this is not a link, but you can tell it explicitly what type it is
var myInterface2 interface{} = myInterface.(myType) If the type is unknown to you, only reflect can help you, but it is slow and leads to a large number of errors =)
https://play.golang.org/p/W43YP7MGSU
package main import ( "fmt" "reflect" ) type MyStruct struct { N int } type MyInterface interface{} func main() { fmt.Println("Hello, playground") var my_struct_1 MyInterface = MyStruct{N: 1} var my_struct_2 MyInterface = my_struct_1.(MyStruct) fmt.Println(my_struct_1, my_struct_2) t := reflect.TypeOf(my_struct_1) fmt.Println(t) ss2 := reflect.ValueOf(my_struct_1).Convert(t) fmt.Println(ss2) fmt.Printf("%T %#v\n", ss2, ss2) // Увы он останется reflect.Value типом, пока, опять же, не сделаешь явное преобразование ss2.Interface().(MyStruct) ss2_MyStruct := ss2.Interface().(MyStruct) fmt.Printf("%T %#v\n", ss2_MyStruct, ss2_MyStruct) } Source: https://ru.stackoverflow.com/questions/528290/
All Articles
myInterface2 := *myInterfacecompiler does not know what it means to dereference (*) an interface, because it is not a link, but you can tell it explicitly what type there isvar myInterface2 interface{} = myInterface.(myType)- Darigaaz