如何通过golang中的reflect.Value获得潜在价值? [英] How to get underlying value from a reflect.Value in golang?

查看:150
本文介绍了如何通过golang中的reflect.Value获得潜在价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我发现了一些帮助我开始使用Go(golang)中的反射的代码,但是我无法获取底层值,因此我基本上可以创建 map [string]字符串



最后,我想将结果变成一个 map [string ] interface {} ,但是这个问题有点阻挡我。



我现在的代码:

 包裹主要

进口(
fmt
反映


类型Foo结构{
名字字符串`tag_name:标记1`
姓氏字符串`tag_name:标记2`
年龄int`tag_name:标记3`
}

func inspect(f interface {})map [string]字符串{
$ bm:= make(map [string] string)
val:= reflect.ValueOf(f).Elem()

for i:= 0;我< val.NumField(); i ++ {
valueField:= val.Field(i)
typeField:= val.Type()。Field(i)

f:= valueField.Interface()
val:= reflect.ValueOf(f)
m [typeField.Name] = val.String()
}

return m
}

func dump(m map [string] string){

for k,v:= range m {
fmt.Printf(%s:%s \ n, k $ v



func main(){
f:=& Foo {
名字:Drew,
姓氏:Olson,
年龄:30,
}
$ b $:检查(f)

转储(a)
}

运行代码的输出:

 名字:Drew 
姓氏:Olson
年龄:< int值>

据我所知,FirstName和LastName的输出是实际的reflect.Value对象,但是对于字符串String ()方法只是输出底层的String。我希望获得int并将其更改为一个字符串,但是从relfect包文档中,我不会立即看到如何完成。



Soo ..如何从golang中的reflect.Value中获取底层值?

解决方案

如何解析值是 fmt 包。请参阅此代码

使用上面提到的代码来匹配您的问题将如下所示:

  switch val.Kind(){
case reflect.Int,reflect.Int8,reflect.Int16,reflect.Int32,reflect.Int64:
m [typeField.Name] = strconv。 FormatInt(val.Int(),10)
case reflect.String:
m [typeField.Name] = val.String()
// etc ...
}

基本上你需要检查所有可用种类


So I found some code that help me get started with reflection in Go (golang), but I'm having trouble getting a the underlying value so that I can basically create a map[string]string from a struct and it's fields.

Eventually, I'd like to make the result into a map[string]interface{}, but this one issue is kind of blocking me.

The code I have at the moment:

package main

import (
    "fmt"
    "reflect"
)

type Foo struct {
    FirstName string `tag_name:"tag 1"`
    LastName  string `tag_name:"tag 2"`
    Age       int  `tag_name:"tag 3"`
}

func inspect(f interface{}) map[string]string {

    m := make(map[string]string)
    val := reflect.ValueOf(f).Elem()

    for i := 0; i < val.NumField(); i++ {
        valueField := val.Field(i)
        typeField := val.Type().Field(i)

        f := valueField.Interface()
        val := reflect.ValueOf(f)
        m[typeField.Name] = val.String()
    }

    return m
}

func dump(m map[string]string) {

    for k, v := range m {
        fmt.Printf("%s : %s\n", k, v)
    }
}

func main() {
    f := &Foo{
        FirstName: "Drew",
        LastName:  "Olson",
        Age:       30,
    }

    a := inspect(f)

    dump(a)
}

The output from running the code:

FirstName : Drew
LastName : Olson
Age : <int Value>

From what I understand the output for FirstName and LastName are actual reflect.Value objects but for strings the String() method on value just outputs the underlying String. I'd like to either get the int and change it into a string, but from the relfect package documentation I'm not immediately seeing how that's done.

Soo.... How do I get the underlying value from a reflect.Value in golang?

解决方案

A good example of how to parse values is the fmt package. See this code.

Using the mentioned code to match your problem would look like this:

switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    m[typeField.Name] = strconv.FormatInt(val.Int(), 10)
case reflect.String:
    m[typeField.Name] = val.String()    
// etc...
}

Basically you need to check for all available Kinds.

这篇关于如何通过golang中的reflect.Value获得潜在价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆