当前位置: 首页 > 面试题库 >

golang递归反射

黄向明
2023-03-14
问题内容

我试图递归地反映一个结构,打印出每个字段的类型。在字段是结构片段的情况下,我希望能够识别数组中保存的类型,然后在该类型上进行反映。

这是一些示例代码

package main

import (
    "log"
    "reflect"
)

type child struct {
    Name *string
    Age  int
}

type Parent struct {
    Name     string
    Surname  *string
    Children []*child
    PetNames []string
}

func main() {

    typ := reflect.TypeOf(Parent{})
    log.Printf("This is a : %s", typ.Kind())

    for i := 0; i < typ.NumField(); i++ {
        p := typ.Field(i)
        if !p.Anonymous {
            switch p.Type.Kind() {
            case reflect.Ptr:
                log.Printf("Ptr: %s is a type %s", p.Name, p.Type)
            case reflect.Slice:
                log.Printf("Slice: %s is a type %s", p.Name, p.Type)
                subtyp := p.Type.Elem()
                if subtyp.Kind() == reflect.Ptr {
                    subtyp = subtyp.Elem()
                }
                log.Printf("\tDereferenced Type%s", subtyp)
            default:
                log.Printf("Default: %s is a type %s", p.Name, p.Type)
            }
        }
    }

}

输出看起来像这样:

This is a : struct
Default: Name is a type string
Ptr: Surname is a type *string
Slice: Children is a type []*main.child
    Dereferenced Type main.child
Slice: PetNames is a type []string
    Dereferenced Type string

当我确定字段类型是指针的一部分时,我可以通过调用subtype.Elem()来推断类型。

输出为“ main.child”

如果我然后尝试反映孩子使用

subSubType := reflect.TypeOf(subtyp)
log.Printf("%+v", subSubType)

我得到以下内容:

 *reflect.rtype

如何使用反射API遍历子结构的字段?


问题答案:

这是一种方法。

func printType(prefix string, t reflect.Type, visited map[reflect.Type]bool) {

    // Print the name of this type with opening ( for description.
    fmt.Printf("%s (", t)

    // Traverse elements, adding to description as we go.
elems:
    for {
        switch t.Kind() {
        case reflect.Ptr:
            fmt.Print("ptr to ")
        case reflect.Slice:
            fmt.Print("slice of ")
        case reflect.Array:
            fmt.Printf("array with %d elements of ", t.Len())
        default:
            break elems
        }
        t = t.Elem()
    }

    // Print the kind of the type and the closing ) of the description.
    // In the case of a struct, we print the names of the fields and recurse.
    switch t.Kind() {
    case reflect.Struct:
        fmt.Printf("struct with %d fields)\n", t.NumField())
        if visited[t] {
            // Don't blow up on recursive type definition.
            break
        }
        visited[t] = true
        prefix += "    "
        for i := 0; i < t.NumField(); i++ {
            f := t.Field(i)
            fmt.Print(prefix, f.Name, " ")
            printType(prefix, f.Type, visited)
        }
    default:
        fmt.Printf("%s)\n", t.Kind())
    }
}

func main() {
    printType("", reflect.TypeOf(Parent{}), make(map[reflect.Type]bool))
}

Parent {}的输出具有以下类型:

type child struct {
    Name *string
    Age  int
}

type Parent struct {
    Name     string
    Surname  *string
    Children []*child
    PetNames []string
    Parents  [2]*Parent
    child
}

是:

main.Parent (struct with 6 fields)
    Name string (string)
    Surname *string (ptr to string)
    Children []*main.child (slice of ptr to struct with 2 fields)
        Name *string (ptr to string)
        Age int (int)
    PetNames []string (slice of string)
    Parents [2]*main.Parent (array with 2 elements of ptr to struct with 6 fields)
    child main.child (struct with 2 fields)

游乐场的例子



 类似资料:
  • 问题内容: 我有一个简单的问题…我正在尝试使用切片在Golang中重现此递归数据结构。 现在,我在下面使用带有切片的递归数据结构的“粗糙”源代码,除了我输入的结构是结构而不是结构片之外,其他所有东西都可以正常工作。理想情况下,我希望类型化的递归数据结构是Trie的一部分,其中包含元素Trie {byte,[] Trie}。希望这有意义吗?现在,我有一个Trie struct {byte,[] Tr

  • 问题内容: 我还没有找到满足我的功能特定需求的任何东西,是的,这是用于家庭作业。 所以我有: 前提条件:x.length> 0 我不能让函数返回任何东西,而唯一的参数是数组这一事实使我感到困惑。 我已经尝试过将循环与递归一起使用,但是我尝试过的一切似乎都以生成函数的无限实例结束。 我已经有了一个想法/建议与该函数一起使用另一个函数,但是,当前如何递归地使用原始函数超出了我的范围。 任何帮助表示赞赏

  • 还缺少的是将最后一个节点的next赋值为NULL。 在任何世界里,像这样的东西会起作用吗?它给出了一个运行时/分段错误。

  • 我对函数式编程很陌生,尤其是下面使用的Scheme。我正在尝试使以下函数是递归的,尾递归的。基本上,该函数的作用是对两个字符串的对齐方式进行评分。当给定两个字符串作为输入时,它会比较每个“列”字符,并根据在称为 scorer 的函数中实现的评分方案(由下面的代码中的函数调用)来累积该对齐的分数。 我有一个想法,用一个帮助函数来累积分数,但我不太确定如何去做,因此我该如何让下面的函数尾递归呢?

  • 我做了一个使用递归方法反转单链表的函数。然而,我在执行下面的代码时遇到了一些困难: 我应该如何在ReverseCursive函数/方法中传递第二个参数,以便执行它? 作为第二个参数,我想简单地传递链表的头节点。但是我不知道如何从类的init方法中获取头节点linked_list 我试了几件事,但都解决不了。也许我不太擅长OOP概念。有人能帮我解决这个问题吗?

  • 问题内容: 我已经在一个类的Java项目上工作了一段时间。它是链表(此处称为,包含称为的简单节点)的实现。问题是,一切都必须使用递归算法来完成。我可以用一种方法来做所有的事情: 现在,我的函数只是调用一个带有参数以允许递归的辅助函数。 我的助手功能具有的签名。 目前,我使用堆栈来迭代工作,但这不是规范所要求的。我在C语言中找到了一种算法,该算法可以递归地将其递归逆转并将其转换为Java代码,并且可