Go语言反射实战:提取结构体字段信息

叶初
叶初
发布时间:2026-10-14| 4人浏览过
原创

Go语言的反射机制允许程序在运行时检查和操作变量。本文将通过具体代码示例,展示如何使用 reflect 包获取结构体的字段信息,包括字段名称、类型以及处理指针类型的细节。

实现目标与准备工作

我们需要编写一个工具函数,能够接受任意结构体(或指向结构体的指针),并返回一个映射(map),其中键为字段名,值为字段类型。这在ORM框架、API序列化等场景下非常有用。

第1步:定义业务数据结构

首先定义一个简单的结构体作为数据模型,用于测试反射功能:

package main
import(
  "fmt"
  "reflect"
)

// Data Model
type Dish struct {
  Id  int
  Name string
  Origin string
  Query func()
}

第2步:核心反射逻辑实现

接下来实现 attributes 函数。该函数负责解析传入接口值的底层类型,提取其字段信息:

// Example of how to use Go's reflection
// Print the attributes of a Data Model
func attributes(m interface{}) (map[string]reflect.Type) {
  typ := reflect.TypeOf(m)
  // if a pointer to a struct is passed, get the type of the dereferenced object
  if typ.Kind() == reflect.Ptr{
    typ = typ.Elem()
  }
 
  // create an attribute data structure as a map of types keyed by a string.
  attrs := make(map[string]reflect.Type)
  // Only structs are supported so return an empty result if the passed object
  // isn't a struct
  if typ.Kind() != reflect.Struct {
    fmt.Printf("%v type can't have attributes inspected\n", typ.Kind())
    return attrs
  }
 
  // loop through the struct's fields and set the map
  for i := 0; i < typ.NumField(); i++ {
    p := typ.Field(i)
      if !p.Anonymous {
        attrs[p.Name] = p.Type
      }
     }
  return attrs
}

关键逻辑说明:

  • 解引用指针:如果输入是指针类型(reflect.Ptr),使用 Elem() 获取其指向的实际类型。
  • 类型检查:确保当前类型是 reflect.Struct,否则直接返回空映射。
  • 过滤嵌入字段:通过 p.Anonymous 判断是否为嵌入字段(anonymous/embedded fields),通常我们只关心显式声明的字段。
  • 遍历字段:使用 NumField() 获取数量,并通过索引获取每个字段的详细信息。

第3步:调用并验证结果

在 main 函数中实例化并测试上述方法:

func main(){
  // iterate through the attributes of a Data Model instance
  for name, mtype := range attributes(&Dish{}) {
    fmt.Printf("Name: %s, Type %s\n", name, mtype.Name())
  }
}

运行后将输出:

Name: Id, Type int
Name: Name, Type string
Name: Origin, Type string
Name: Query, Type func()

完成判断与注意事项

当控制台正确打印出结构体所有非嵌入字段的名称和对应类型时,即表示反射逻辑工作正常。需要注意的是,Go的反射属于运行时特性,虽然强大但会有一定的性能开销,建议在高性能循环中谨慎使用。

以上就是Go语言中使用反射的方法详细内容,更多关于Go语言反射的资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。