This is a creation in Article, where the information may have evolved or changed.
Reflective packets have an interface: type, and a struct value;
Type interface
The Commontype type implements the type interface, and the following are the various types of relationships in type
- Commontype>uncommontype>method
- Arraytype|chantype|functype|interfacetype|maptype|ptrtype|slicetype >commontype
- Ptrmap>n*commontype
Other structures
Method structure
Methodbyname () and method () return this type
Type Method struct {Name stringpkgpath stringtype Type //Method typefunc Value//func with receiver as F irst argumentindex int //index for Type.method}
Structfield structure
Field () and Fieldbyindex (), and Fieldbyname (), FIELDBYNAMEFUNC returns the type
Type Structfield struct {name *string //nil for embedded Fieldspkgpath *string //nil for exported Names; oth Erwise Import Pathtyp *runtimetype//type of Fieldtag *string //nil If no tagoffset uintptr //Byte Offset of field within struct}
Reflects the type of a variable, essentially converting the pointer of the variable to a commontype pointer
- First, a pointer to a variable is converted to the pointer type by unsafe package
- Then convert the pointer type to the *emptyinterface type and use the * expression to pass its Emptyinterface value to Eface
- Asserts whether the value of Eface.typ is a pointer type of Commontype and returns its value if it is
Func ToType (P *runtimetype) Type {if p = = Nil {return Nil}return (*p). *commontype)}func TypeOf (i interface{}) Type {eface: = * (*emptyinterface) (unsafe. Pointer (&i)) return ToType (Eface.typ)}
Introduction to type Functions
-
Func chanof (dir chandir, T type) type
-
Return Channel type
-
Func mapof (key, Elem type) type
-
Return to map type
-
Func Ptrto (t type) type
-
Return pointer type
-
Func sliceof (t type) type
-
return Slice Type
-
Func TypeOf (i interface{}) Type
-
reflective variable type, it is best not to pass the pointer directly. Otherwise something will not be emitted. For example, name ()
Type method Introduction
-
-
Func (t *commontype) Align () int
-
The alignment of the
-
bytes returned when the type is in content allocation
-
-
Func (t *commontype) fieldalign () int
-
-
when the type is a field of a structure, his byte alignment
-
-
Func (t *commontype) method (i int) (M method)
-
-
returns a method type by shaping the index, reflection type
Type B struct {c stringb bytea Int}func (b b) test () {}func main () {B: = b{}fmt. Println (reflect. TypeOf (b). Method (0). Name) //test}
-
-
Func (t *commontype) methodbyname (name string) (M Method, OK BOOL)
-
-
determines whether the type has the method by the method name, returns the method if it exists, and returns an OK value of true
Type B struct {c stringb bytea Int}func (b b) test () {}func main () {b: = new (b) m, _: = Reflect. TypeOf (b). Methodbyname ("Test") fmt. Println (M.pkgpath)}
-
-
Func (t *commontype) nummethod () int
-
-
returns the number of methods owned by this type
-
-
Func (t *commontype) Name () string
-
-
returns the name of the type, if the anonymous type will return a null character channeling, and if it is a pointer, nothing
-
-
Func (t *commontype) Pkgpath () string
-
-
returns the path of the package that contains the type, and if it is a pointer, what the wood has
-
-
Func (t *commontype) Size () uintptr
-
-
returns the size of the type
-
-
Func (t *commontype) string () string
-
-
return type of character channeling name
-
-
Func (t *commontype) Kind () Kind
-
-
returns the special kind of this type, (Struct,ptr,func, etc.)
-
-
Func (t *commontype) Implements (U Type) bool
-
-
determines whether the type implements the U interface. Note You must not be nil and is an interface
-
-
Func (t *commontype) assignableto (U Type) bool
-
-
determines whether the type can be assigned to the U type
-
-
Func (t *commontype) Bits () int
-
The
-
use of the reflection type of bytes, if not the Int,uint,float,complex species will produce panic
-
-
Func (t *commontype) Chandir () Chandir
-
-
reflected Channel directory
-
-
Func (t *commontype) isvariadic () bool
-
-
determine if the function has a mutable parameter (...)
Type B struct {c stringb bytea Int}func (b b) test () {}func test (a ... int) {}func main () {FMT. Println (reflect. TypeOf (Test). Isvariadic ()) //truefmt. Println (reflect. TypeOf (b.test). Isvariadic ()) //false}
-
-
Func (t *commontype) Elem () Type
-
-
Returns a type of element type, the kind of non-array,chan,map,ptr,slice will generate panic
Type B struct {c stringb bytea Int}func (b b) test () {}func main () {B: = &b{}fmt. Println (reflect. TypeOf (b). Elem ()) //main. B
-
-
Func (t *commontype) Field (i int) Structfield
-
-
returns a structure field based on the index, with a value of Structfield type
-
-
Func (t *commontype) fieldbyindex (index []int) Structfield
-
-
returns the field structure of a nested type.
Type A struct {a intb Bytec string}type b struct {Ac stringb bytea int}func (b b) test () {}func main () {b: = B{}index: = [ ]int{0, 1}fmt. Println (reflect. TypeOf (b). Fieldbyindex (Index). Name) //b}
-
-
Func (t *commontype) fieldbyname (name string) (Structfield, BOOL)
-
-
reflect the field type, depending on the field name
-
-
Func (t *commontype) Fieldbynamefunc (match func (string) bool) (Structfield, BOOL)
-
-
.... I don't understand
.
Type B struct {c stringb bytea Int}func Test (a string) bool {return True}func main () {b: = b{}fmt. Println (reflect. TypeOf (b). Fieldbynamefunc (test)) //{ 0 [] false} false}
-
Func (t *commontype) in (i int) Type
-
The I parameter of the reflection function type
Func Test (a string) bool {return True}func main () {FMT. Println (reflect. TypeOf (Test). In (0))}
-
Func (t *commontype) Key () Type
-
The type of key that reflects the map type, if not map, generates panic.
-
Func (t *commontype) Len () int
-
Reflects the length of the array type, which can cause panic if it is not an array
-
Func (t *commontype) Numfield () int
-
Reflects the number of fields in a struct, which can cause panic if not a struct.
-
Func (t *commontype) numin () int
-
Reflection of the number of input parameters of a func, if not the function will cause panic.
-
Func (t *commontype) numout () int
-
The number of returned results of a reflection function, a stake is not a function that creates panic.
-
Func (t *commontype) out (i int) Type
The
-
reflection function returns the type of I of the result and, if not the function, creates a panic, and if not in the range [0, Numout ()], it can also cause panic
.
Off-topic: Declaring a variable whose value is an anonymous structure
Type T struct {}var T Tvar t struct {}