如何将接口转换为结构

Here is the simplified code of a cache. Suppose Container placed in a package, so it don't know about Member. While I wanna store instances of Member in Container, So I store an empty instance of Member in Container as outerType. In the Container->GetMysql, I fill a new variable by test values (but, in real world, It fill by data of database, dynamically). then in the function Put, I store data in items as Cache for next uses. In the Get I get the data stored in the items. Before this every thing is fine. My problem is where i want to convert result of Get to type of Member m = res.(Member) . How Can I convert it to an instance of Member I found many question about this subject, but none of them solved my problem

For more detail: I want the Get return data with its pointer of where it stored in items. So if I get some variable of same member, an change in one are shown in others

package main

import (
    "fmt"
    "reflect"
)

type Member struct {
    Id     int
    Name   string
    Credit int
    Age    int
}

type Container struct {
    outerType interface{}
    items     map[string]*interface{}
}

func (cls *Container)GetMysql(s string, a int64) interface{}{
    obj := reflect.New(reflect.TypeOf(cls.outerType))
    elem := obj.Elem()
    //elem := reflect.ValueOf(o).Elem()
    if elem.Kind() == reflect.Struct {
        f := elem.FieldByName("Name")
        f.SetString(s)
        f = elem.FieldByName("Credit")
        f.SetInt(a)
    }
    return obj.Interface()
}

func (cls *Container)Get(value string) *interface{}{
    return cls.items[value]
}

func (cls *Container)Put(value string, a int64) {
    res := cls.GetMysql(value, a)
    cls.items[value] = &res
}

func main() {
    c := Container{outerType:Member{}}
    c.items = make(map[string]*interface{})
    c.Put("Jack", 500)
    res := c.Get("Jack")
    fmt.Println(*res)
    m := &Member{}
    m = res.(Member) // Here is the problem. How to convert ?
    fmt.Println(m)
}

You should hardly ever use pointer to interface. My advice is to never use it, when you'll need it, you'll know.

Instead if you need a pointer to something (so you can have the same pointer at multiple places, and so modifying the pointed value somewhere, it will have effect on the others), "wrap the pointer" in the interface value.

So first modify the items field so that it stores interface{} values instead of pointers:

items map[string]interface{}

This means no restriction: you can pass and store pointers, that's not a problem.

Next modify Get() to return interface{}:

func (cls *Container) Get(value string) interface{}{
    return cls.items[value]
}

And also in Put(), don't take the address of an interface{}:

func (cls *Container) Put(value string, a int64) {
    res := cls.GetMysql(value, a)
    cls.items[value] = res
}

And you have to type-assert *Member from the values returned by Get().

And now testing it:

c := Container{outerType: Member{}}
c.items = make(map[string]interface{})
c.Put("Jack", 500)
res := c.Get("Jack")
fmt.Println(res)
m := res.(*Member) // Here is the problem. How to convert ?
fmt.Println(m)

Output (try it on the Go Playground):

&{0 Jack 500 0}
&{0 Jack 500 0}

Now if you would modify a field of m:

m.Credit = 11

And then get the value form the cache:

fmt.Println(c.Get("Jack"))

We'll see the modified value, even though we did not call Put() (try it on the Go Playground):

&{0 Jack 11 0}