Skip to content

Instantly share code, notes, and snippets.

@e7
Created April 7, 2021 12:31
Show Gist options
  • Save e7/149af9faf4fa7c6112d09a8b42a719ac to your computer and use it in GitHub Desktop.
Save e7/149af9faf4fa7c6112d09a8b42a719ac to your computer and use it in GitHub Desktop.
package main
import (
"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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment