0
ライブラリは、私はGET
方法はおそらく仕事ができる方法を理解することはできません、インターフェイスであるparamを変更するには?私はDBと連携して使用してい
Put(c context.Context, key *Key, src interface{}) (*Key, error)
Get(c context.Context, key *Key, dst interface{}) error
しかしキャストなし/負荷データを保存するために便利なインターフェイスを提供します。私は最も単純なスニペットで動作を複製しようとしましたが、動作しませんでした。彼らはGet
機能がinterface{}
を受け入れて、ポインタの先を変更させる方法
import "fmt"
type MyType struct {
inside string
}
func setVal(dst *MyType) {
someVal := MyType{"new"}
*dst = someVal
}
func setValGen(dst interface{}) {
someVal := MyType{"new"}
dst = someVal
}
func main() {
typeDstA := MyType{"old"}
setVal(&typeDstA)
fmt.Println(typeDstA) //changed to new
typeDstB := MyType{"old"}
setValGen(&typeDstB)
fmt.Println(typeDstB) //remains old
}
?
重複:[反射との界面の下ポインタのタイプと値を変更します(https://stackoverflow.com/questions/46342228/changing-pointer-type-and-value-アンダーインタフェースと反射/ 46342394#46342394)。 – icza