繁体   English   中英

使用Go中的反射来获取结构的名称

[英]using reflection in Go to get the name of a struct

我发现这个问题有这个很好的答案:

如何在Golang中找到一个对象的类型?

我玩弄了答案并尝试以相同的方式获取结构的名称:

package main

import (
        "fmt"
        "reflect"
)

type Ab struct {

}

func getType(myvar interface{}) string {
        return reflect.TypeOf(myvar).Name()
}

func main() {
        fmt.Println("Hello, playground")

        tst := "string"
        tst2 := 10
        tst3 := 1.2
        tst4 := new(Ab)

        fmt.Println(getType(tst))
        fmt.Println(getType(tst2))
        fmt.Println(getType(tst3))
        fmt.Println(getType(tst4))

}

去游乐场: http//play.golang.org/p/tD8mygvETH

但输出是:

Hello, playground
string
int
float64


Program exited.

预期产出将是:

Hello, playground
string
int
float64
Ab

Program exited.

我试着通过阅读文档来弄清楚,但没有找到相关的问题。 所以,对于非常普遍的问题感到抱歉,但是:

是什么原因, reflect.TypeOf().Name()不适用于(this)struct(s)?

在您的示例中,您传递指针类型( *Ab )的值,而不是结构类型。

坚持Type.Name()

如果它不是指针, Type.Name()将正确返回Ab 如果仍然需要结构名称,则可以使用Type.Elem()来获取元素的类型:

func getType(myvar interface{}) string {
    if t := reflect.TypeOf(myvar); t.Kind() == reflect.Ptr {
        return "*" + t.Elem().Name()
    } else {
        return t.Name()
    }
}

测试它:

tst4 := Ab{}
tst5 := new(Ab)
fmt.Println(getType(tst4))
fmt.Println(getType(tst5))

输出(在Go Playground上尝试修改后的示例):

Ab
*Ab

注意:

请注意,由于Type.Name()不解析指针,如果传递的值是指向指针的指针(例如**Ab ,则Type.String() ,而当Type.String()自动解析指针时,也会在这种情况下起作用。

我们可以轻松地使我们的getType()函数与**Ab一起使用(或者使用任何深度的指针):

func getType(myvar interface{}) (res string) {
    t := reflect.TypeOf(myvar)
    for t.Kind() == reflect.Ptr {
        t = t.Elem()
        res += "*"
    }
    return res + t.Name()
}

用值调用它:

tst4 := Ab{}
tst5 := new(Ab)
tst6 := &tst5 // type of **Ab
tst7 := &tst6 // type of ***Ab

输出(在Go Playground上试试):

Ab
*Ab
**Ab
***Ab

使用Type.String()

一种更简单和更好的方法是使用Type.String()而不是Type.Name() ,它自动处理指针并且还包括包名。 例如:

func getType(myvar interface{}) string {
    return reflect.TypeOf(myvar).String()
}

对于修改后的示例,它输出:

string
int
float64
main.Ab
*main.Ab

Go Playground上试试这个变种。

fmt也有很酷的%T标签

package main

import (
    "fmt"
    "net/http"
)

type Potato struct {
}

func main() {
    fmt.Printf("I have a %T, an %T and a %T\n", Potato{}, http.StatusMultipleChoices, &http.Response{})
}

输出I have a main.Potato, an int and a *http.Response https://play.golang.org/p/6z7_0BSitm

问题是new返回指针,下面应该得到所需的结果。

package main

import (
    "fmt"
    "reflect"
)

type Ab struct {
}

func getType(myvar interface{}) {
    valueOf := reflect.ValueOf(myvar)

    if valueOf.Type().Kind() == reflect.Ptr {
        fmt.Println(reflect.Indirect(valueOf).Type().Name())
    } else {
        fmt.Println(valueOf.Type().Name())
    }
}

func main() {
    fmt.Println("Hello, playground")

    tst := "string"
    tst2 := 10
    tst3 := 1.2
    tst4 := new(Ab)

    getType(tst)
    getType(tst2)
    getType(tst3)
    getType(tst4)

}

输出是

Hello, playground
string
int
float64
Ab

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM