1. Home
  2. Docs
  3. golang
  4. 内置库(包-package)
  5. reflect

reflect

reflect包描述

包reflect实现了运行时反射,允许程序操作具有任意类型的对象。典型的用法是取一个具有静态类型interface{}的值,通过调用TypeOf来提取其动态类型信息,TypeOf返回一个Type。

对ValueOf的调用返回一个代表运行时数据的Value。Zero接收一个类型并返回一个代表该类型的零值的Value。

type Type

func TypeOf

func TypeOf(i interface{}) Type


TypeOf returns the reflection Type that represents the dynamic type of i. If i is a nil interface value, TypeOf returns nil.

TypeOf返回代表i的动态类型的反射类型。如果i是一个无接口的值,TypeOf返回nil。

type Value

func ValueOf

func ValueOf(i interface{}) Value


ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value.

ValueOf返回一个新的Value,初始化为存储在接口i中的具体值。ValueOf(nil)返回零值。

func (Value) NumField

func (v Value) NumField() int


NumField returns the number of fields in the struct v. It panics if v’s Kind is not Struct.

NumField返回结构体v中的字段数,如果v的Kind不是Struct,会报panics。

func (Value) Kind

func (v Value) Kind() Kind


Kind returns v’s Kind. If v is the zero Value (IsValid returns false), Kind returns Invalid.

Kind返回v的Kind。如果v是零值(IsValid返回false),Kind返回Invalid。

type StructTag

func (StructTag) Get

func (tag StructTag) Get(key string) string


Get returns the value associated with key in the tag string. If there is no such key in the tag, Get returns the empty string. If the tag does not have the conventional format, the value returned by Get is unspecified. To determine whether a tag is explicitly set to the empty string, use Lookup.

Get 返回与标签字符串中的 key 相关的值。如果标签中没有这样的键,Get 会返回空字符串。如果标记没有常规格式,Get 返回的值则未指定。要确定一个标记是否被明确地设置为空字符串,请使用 Lookup。

Was this article helpful to you? Yes No

How can we help?