Created
July 10, 2018 06:57
-
-
Save aimuz/2047df2b0fcccc5308e29ea363425a50 to your computer and use it in GitHub Desktop.
反射获取类型,并且动态创建
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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