I am trying to use type assertion in golang. with direct assertion there is no problem.
a, ok := i.(MyStruct)
but when I use reflection
b, ok := i.(reflect.TypeOf(i))
I got an error. What was a problem with that? and How to deal with it?
Full Code:
package main
import (
"fmt"
"reflect"
)
type MyStruct struct {
field string
}
func main() {
var i interface{} = MyStruct{field:"Thanks"}
a, ok := i.(MyStruct)
fmt.Println(a, ok)
t := reflect.TypeOf(i)
fmt.Println(t)
b, ok := i.(t)
fmt.Println(b, ok)
}
Thank for your answers.