golang convert interface to concrete type
package main
import (
"fmt"
"reflect"
)
type MyStruct struct {
Name string
Age int
}
func main() {
var myInterface interface{} = MyStruct{"John Doe", 25}
// Step 1: Use type assertion to convert interface to concrete type
myConcreteType, ok := myInterface.(MyStruct)
if !ok {
fmt.Println("Conversion failed")
return
}
// Step 2: Access fields of the concrete type
name := myConcreteType.Name
age := myConcreteType.Age
// Step 3: Print the results
fmt.Printf("Name: %s, Age: %d\n", name, age)
}