You can emulate the AppEngine datastore interface with reflect; I usually say minimize reflection, but you (and AppEngine and other ORMs) have no other great option to present the interface you want. For something emulating Getyou:
- get
reflect.ValuewithValueOf() - ,
reflect.Zeroreflect.Field() ..reflect.Indirect() Value.Set(), .
, , http://play.golang.org/p/g7dNlrG_vr :
package main
import (
"fmt"
"reflect"
)
func main() {
i := 1
clear(&i)
fmt.Println(i)
}
func clear(dst interface{}) {
dstPtrValue := reflect.ValueOf(dst)
dstPtrType := dstPtrValue.Type()
dstType := dstPtrType.Elem()
dstValue := reflect.Indirect(dstPtrValue)
zeroValue := reflect.Zero(dstType)
dstValue.Set(zeroValue)
}
GetMulti . : http://play.golang.org/p/G_6jit2t-2 :
package main
import (
"fmt"
"reflect"
)
func main() {
s := []int{}
getMultiZeroes(&s, 10)
fmt.Println(s)
}
func getMultiZeroes(slicePtrIface interface{}, howMany int) {
slicePtrValue := reflect.ValueOf(slicePtrIface)
slicePtrType := slicePtrValue.Type()
sliceElemType := slicePtrType.Elem().Elem()
sliceValue := reflect.Indirect(slicePtrValue)
sliceElemValue := reflect.Zero(sliceElemType)
for i := 0; i < howMany; i++ {
sliceValue.Set(reflect.Append(sliceValue, sliceElemValue))
}
}
( , ), ( ), ; , . GetMulti http://play.golang.org/p/q-9WyUqv6P :
package main
import "fmt"
func main() {
s := []int{}
getZeroes(&s)
fmt.Println(s)
fails := []float32{}
getZeroes(&fails)
}
func getZeroes(slicePtrIface interface{}) {
switch sp := slicePtrIface.(type) {
case *[]int:
(*sp) = append((*sp), 0, 0)
case *[]string:
(*sp) = append((*sp), "", "")
default:
panic(fmt.Sprintf("getZeroes: passed type %T, which is not a pointer to a slice of a supported type", slicePtrIface))
}
}
; reflect . http://play.golang.org/p/6qw52B7eC3 ( , ).
, , GetMulti, GetMulti, .
, :
" " , . Go , , . , " ", , Go (int, struct) . ++ : ++ void f(i int&) { i++; } i , callsite. func (i int) { i++ } .
Go , , , . ++ " " ; , .
, , , , . , . , ++, Go this - & . Russ Cox godata post , .