深拷贝

func DeepCopy(a, b interface{}) {
    buff := new(bytes.Buffer)
    enc, dec := gob.NewEncoder(buff), gob.NewDecoder(buff)
    if err := enc.Encode(a); err != nil {
        panic(err)
    }
    if err := dec.Decode(b); err != nil {
        panic(err)
    }
}

func Replica(a interface{}) interface{} {
    rv := reflect.ValueOf(a)
    for rv.Kind() == reflect.Ptr {
        rv = rv.Elem()
    }
    b := reflect.New(rv.Type())
    DeepCopy(a, b.Interface())
    return b.Elem().Interface()
}

你可能感兴趣的:(深拷贝)