reflect.Value接受
interface{},并且interface{}到的值不能用于更改原始值。否则,struct当您甚至不打算向其传递指针时,您可能最终会在代码中更改数据。(或者,在这种情况下,更改按值传递的切片的长度。)因此,如果使用地址,则必须在之前进行操作
ValueOf。
要指向可以传递给切片的包的指针
append(例如
modlGoogle App Engine
GetMulti),可以使用http://play.golang.org/p/1ZXsqjrqa3之类的东西,复制到此处:
package mainimport ( "fmt" "reflect")type row struct { i, j int }func main() { aRow := row{} valueType := reflect.ValueOf(aRow).Type() slicePtrVal := reflect.New(reflect.SliceOf(valueType)) slicePtrIface := slicePtrVal.Interface() getQueryResults(slicePtrIface) fmt.Println(slicePtrIface)}// standing in for `modl` or whatever populates the slicefunc getQueryResults(slicePtr interface{}) { sPtr := slicePtr.(*[]row) (*sPtr) = append((*sPtr), row{1,3}) }reflect.Value自己添加到一个切片上需要使用另外几行
reflect,但听起来您正在使用的程序包会为您完成这一部分。有关常规信息,请执行附加操作的代码位于http://play.golang.org/p/m3-xFYc6ON及以下:
package mainimport ( "fmt" "reflect")type row struct { i, j int }func main() { aRow := row{} // make a pointer to an empty slice rowType := reflect.ValueOf(aRow).Type() slicePtrVal := reflect.New(reflect.SliceOf(rowType)) slicePtrIface := slicePtrVal.Interface() // append a zero row to it rowVal := reflect.Zero(rowType) sliceval := reflect.Indirect(slicePtrVal) sliceval.Set(reflect.Append(sliceval, rowVal)) fmt.Println(slicePtrIface)}


