How do you make two interfaces reference the same object?
I would like to make two interfaces reference the same object.
package main
import (
"fmt"
"reflect"
)
func main(){
var a, b any
a = "Hi"
b = reflect.ValueOf(&a).Elem()
a = reflect.ValueOf(&b).Elem()
b = "Howdy"
fmt.Println(a)
fmt.Println(b)
}
PRINT LOGS
Howdy
Howdy
PLAYGROUND: https://go.dev/play/p/qizVO42UaUj
This code works as intended, aside from the fact that a
and b
are not interfaces. So when I convert them to interfaces like so...
package main
import (
"fmt"
"reflect"
)
func main(){
var a, b any
a = "Hi"
b = reflect.ValueOf(&a).Elem().Interface()
a = reflect.ValueOf(&b).Elem().Interface()
b = "Howdy"
fmt.Println(a)
fmt.Println(b)
}
PRINT LOGS
Hi
Howdy
PLAYGROUND: https://go.dev/play/p/jCpuepBJYdD
...the code no longer works as intended. Is there a way to convert from the reflect.Value data type to the interface data type while maintaining that the two variables reference the same object?
Comments
Post a Comment