Skip to content

Instantly share code, notes, and snippets.

@aimuz
Created July 10, 2018 06:57
Show Gist options
  • Save aimuz/2047df2b0fcccc5308e29ea363425a50 to your computer and use it in GitHub Desktop.
Save aimuz/2047df2b0fcccc5308e29ea363425a50 to your computer and use it in GitHub Desktop.
反射获取类型,并且动态创建
package main
import (
"fmt"
"reflect"
)
type My struct {
Name string
Id int
}
func main() {
my := &My{}
myType := reflect.TypeOf(my)
slice := reflect.MakeSlice(reflect.SliceOf(myType), 10, 10).Interface()
p := slice.([]*My)
fmt.Printf("%T %d %d\n", p, len(p), cap(p))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment