反射是 Go 的难度比较大的知识点之一。我会尽量介绍的时候简单一点。

本教程有以下几节。

  • 什么是反射?

  • 检查变量并找到其类型需要的是什么?

  • 反射的包

    • reflect.Type 和 reflect.Value

    • reflect.Kind

    • NumField() 和 Field() 方法

    • Int() 和 String() 方法

  • 完整的程序

  • 是否应该使用反射?

现在让我们一一讨论这些部分。

什么是反射?

反射是程序在运行时检查变量和值并获取到它们的类型的能力。你可能不明白这是什么意思,但没关系。在本教程的最后,你将对反射会有一个清晰的了解,所以请继续往下面看。

检查变量并找到其类型的条件是什么?

在学习反射时,任何人的第一个问题是为什么我们需要检查变量并在运行时获取它的类型?当我们的程序中的每个变量都由我们定义时,我们在编译时就知道它的类型。嗯,大部分时候都是这样,但并非总是如此。

让我解释一下我的意思。我们来写一个简单的程序。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. i := 10
  7. fmt.Printf("%d %T", i, i)
  8. }

Run in playground

在上面的程序中,i 的类型在编译时是已知的,我们在下一行打印它。这里没什么好奇怪的。

现在让我们了解在运行时知道变量类型的必要性。假设我们想编写一个简单的函数,它将 struct 作为参数,并使用它创建一个 SQL 插入查询。

思考下面的程序,

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type order struct {
  6. ordId int
  7. customerId int
  8. }
  9. func main() {
  10. o := order{
  11. ordId: 1234,
  12. customerId: 567,
  13. }
  14. fmt.Println(o)
  15. }

Run in playground

我们需要编写一个函数,它将上面程序中的 struct o 作为参数并返回以下 SQL 插入查询,

  1. insert into order values(1234, 567)

这个函数很容易编写。让我们现在就开始做。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type order struct {
  6. ordId int
  7. customerId int
  8. }
  9. func createQuery(o order) string {
  10. i := fmt.Sprintf("insert into order values(%d, %d)", o.ordId, o.customerId)
  11. return i
  12. }
  13. func main() {
  14. o := order{
  15. ordId: 1234,
  16. customerId: 567,
  17. }
  18. fmt.Println(createQuery(o))
  19. }

Run in playground

第一行中的 createQuery 函数。 第12行中使用 oordIdcustomerId 字段创建插入查询。该程序将输出,

  1. insert into order values(1234, 567)

现在让我们的查询创建进入下一个阶段。如果我们想要使我们的查询创建程序适用于任何结构,该怎么办?让我解释一下这是什么意思。

  1. package main
  2. type order struct {
  3. ordId int
  4. customerId int
  5. }
  6. type employee struct {
  7. name string
  8. id int
  9. address string
  10. salary int
  11. country string
  12. }
  13. func createQuery(q interface{}) string {
  14. }
  15. func main() {
  16. }

上面程序第 16 行中,我们的目标是在第一行完成 createQuery 函数。以便它将任何 struct 作为参数,并基于 struct 字段创建插入查询。

例如,如果我们传递下面的结构,

  1. o := order {
  2. ordId: 1234,
  3. customerId: 567
  4. }

我们的 createQuery 函数应该返回,

  1. insert into order values (1234, 567)

同样,如果我们通过

  1. e := employee {
  2. name: "Naveen",
  3. id: 565,
  4. address: "Science Park Road, Singapore",
  5. salary: 90000,
  6. country: "Singapore",
  7. }

它应该返回

  1. insert into employee values("Naveen", 565, "Science Park Road, Singapore", 90000, "Singapore")

由于 createQuery 函数应该与任何结构一起使用,因此它将 interface{} 作为参数。为简单起见,我们只处理包含 stringint 类型字段的结构,但这可以扩展为任何类型。

createQuery 函数应该适用于任何结构。编写此函数的唯一方法是检查在运行时传递给它的 struct 参数的类型,找到它的字段然后创建查询。这是反射有用的地方。在本教程的后续步骤中,我们将学习如何使用 reflect 包实现此目的。

reflect package

反射包在 Go 中实现运行时反射。反射包有助于识别底层具体类型和 interface{} 变量的值。这正是我们所需要的。 createQuery 函数采用 interface{} 参数,需要根据 interface{} 参数的具体类型和值创建查询。这正是反射包所帮助做的。

在编写通用查询生成器程序之前,我们需要首先了解反射包中的一些类型和方法。让我们一个一个介绍。

reflect.Type and reflect.Value

interface{} 的具体类型由 reflect.Type 表示,底层值由 reflect.Value 表示。有两个函数 reflect.TypeOf() reflect.ValueOf() 分别返回 reflect.Type 和 reflect.Value。这两种类型是创建查询生成器的基础。让我们编写一个简单的例子来理解这两种类型。

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type order struct {
  7. ordId int
  8. customerId int
  9. }
  10. func createQuery(q interface{}) {
  11. t := reflect.TypeOf(q)
  12. v := reflect.ValueOf(q)
  13. fmt.Println("Type ", t)
  14. fmt.Println("Value ", v)
  15. }
  16. func main() {
  17. o := order{
  18. ordId: 456,
  19. customerId: 56,
  20. }
  21. createQuery(o)
  22. }

Run in playground

在上面的程序中,第 13 行 createQuery 函数将 interface{} 作为参数。 14 行函数reflect.TypeOf 将 interface{} 作为参数,并返回包含传递的 interface{} 参数的具体类型的 reflect.Type。类似地,第 15 行 reflect.ValueOf 函数将 interface {} 作为参数,并返回 reflect.Value,其中包含传递的 interface{} 参数的基础值。

以上程序打印,

  1. Type main.order
  2. Value {456 56}

从输出中,我们可以看到程序打印具体类型和接口的值。

reflect.Kind

在名称为 Kind 的反射包中有一个更重要的类型。

反射包中的 KindType 可能看起来相似,但它们之间存在差异,这将从下面的程序中清楚地看出。

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type order struct {
  7. ordId int
  8. customerId int
  9. }
  10. func createQuery(q interface{}) {
  11. t := reflect.TypeOf(q)
  12. k := t.Kind()
  13. fmt.Println("Type ", t)
  14. fmt.Println("Kind ", k)
  15. }
  16. func main() {
  17. o := order{
  18. ordId: 456,
  19. customerId: 56,
  20. }
  21. createQuery(o)
  22. }

Run in playground

上面的程序输出,

  1. Type main.order
  2. Kind struct

我想你现在会清楚两者之间的差异。 Type 表示 interface{} 的实际类型,在这种情况下,main.OrderKind 表示类型的特定种类。在这种情况下,它是一个 struct

NumField() 和 Field()方法


NumField() 方法返回结构中的字段数,Field(i int) 方法返回第 i 个字段的reflect.Value

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type order struct {
  7. ordId int
  8. customerId int
  9. }
  10. func createQuery(q interface{}) {
  11. if reflect.ValueOf(q).Kind() == reflect.Struct {
  12. v := reflect.ValueOf(q)
  13. fmt.Println("Number of fields", v.NumField())
  14. for i := 0; i < v.NumField(); i++ {
  15. fmt.Printf("Field:%d type:%T value:%v\n", i, v.Field(i), v.Field(i))
  16. }
  17. }
  18. }
  19. func main() {
  20. o := order{
  21. ordId: 456,
  22. customerId: 56,
  23. }
  24. createQuery(o)
  25. }

Run in playground

在上面的程序中,我们第 14 行首先检查 qKind 是否是 struct ,因为 NumField 方法仅适用于 struct 。 剩下的程序一目了然。 该程序输出,

  1. Number of fields 2
  2. Field:0 type:reflect.Value value:456
  3. Field:1 type:reflect.Value value:56


Int() 和 String() 方法

IntString 方法有助于将 reflect.Value 分别提取为 int64string

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. a := 56
  8. x := reflect.ValueOf(a).Int()
  9. fmt.Printf("type:%T value:%v\n", x, x)
  10. b := "Naveen"
  11. y := reflect.ValueOf(b).String()
  12. fmt.Printf("type:%T value:%v\n", y, y)
  13. }

Run in playground

在上面的程序中,第 11 行我们将 reflect.Value 提取为 int64,然后第 13 行将其提取为 string。 这个程序打印,

  1. type:int64 value:56
  2. type:string value:Naveen


完整的程序

现在我们已经有足够的知识来成查询生成器,让我们继续做下去

  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type order struct {
  7. ordId int
  8. customerId int
  9. }
  10. type employee struct {
  11. name string
  12. id int
  13. address string
  14. salary int
  15. country string
  16. }
  17. func createQuery(q interface{}) {
  18. if reflect.ValueOf(q).Kind() == reflect.Struct {
  19. t := reflect.TypeOf(q).Name()
  20. query := fmt.Sprintf("insert into %s values(", t)
  21. v := reflect.ValueOf(q)
  22. for i := 0; i < v.NumField(); i++ {
  23. switch v.Field(i).Kind() {
  24. case reflect.Int:
  25. if i == 0 {
  26. query = fmt.Sprintf("%s%d", query, v.Field(i).Int())
  27. } else {
  28. query = fmt.Sprintf("%s, %d", query, v.Field(i).Int())
  29. }
  30. case reflect.String:
  31. if i == 0 {
  32. query = fmt.Sprintf("%s\"%s\"", query, v.Field(i).String())
  33. } else {
  34. query = fmt.Sprintf("%s, \"%s\"", query, v.Field(i).String())
  35. }
  36. default:
  37. fmt.Println("Unsupported type")
  38. return
  39. }
  40. }
  41. query = fmt.Sprintf("%s)", query)
  42. fmt.Println(query)
  43. return
  44. }
  45. fmt.Println("unsupported type")
  46. }
  47. func main() {
  48. o := order{
  49. ordId: 456,
  50. customerId: 56,
  51. }
  52. createQuery(o)
  53. e := employee{
  54. name: "Naveen",
  55. id: 565,
  56. address: "Coimbatore",
  57. salary: 90000,
  58. country: "India",
  59. }
  60. createQuery(e)
  61. i := 90
  62. createQuery(i)
  63. }

Run in playground

第 22 行我们首先检查传递的参数是否是 struct。 第 23 行我们使用 Name() 方法从 reflect.Type 获取结构的名称。 在下一行中,我们使用 t 并开始创建查询。

case 语句在第28行检查当前字段是否为 reflect.Int,如果是这种情况,我们使用Int() 方法将该字段的值提取为 int64if else 语句用于处理边界情况。 请添加日志来了解为何需要它。 34 行中类似的逻辑用于提取 string

我们还添加了一些检查,以防止在将不支持的类型传递给 createQuery 函数时程序崩溃。 其余部分是一目了然的。 我建议在适当的位置添加日志并检查其输出以更好地理解该程序。

这个程序输出

  1. insert into order values(456, 56)
  2. insert into employee values("Naveen", 565, "Coimbatore", 90000, "India")
  3. unsupported type

我将把它作为练习留给读者,让他们将字段名添加到输出查询中。请尝试将程序更改为打印格式查询

  1. insert into order(ordId, customerId) values(456, 56)


是否应该使用反射?

展示了反射的实际用途,现在才是真正的问题。 你应该使用反射吗? 我想引用 Rob Pike 关于使用反射的格言来回答这个问题。

Clear is better than clever. Reflection is never clear.

在Go中,反射是一个非常强大和先进的概念,应该谨慎使用。 使用反射编写清晰且可维护的代码非常困难。 应尽可能避免使用,并且只有在绝对必要时才应使用反射。

原文链接

https://golangbot.com/reflection/