Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get underlying value from a reflect.Value in golang?

Tags:

reflection

go

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?

like image 936
lucidquiet Avatar asked Aug 06 '13 22:08

lucidquiet


People also ask

What is reflect value Golang?

The reflect. ValueOf() Function in Golang is used to get the new Value initialized to the concrete value stored in the interface i. To access this function, one needs to imports the reflect package in the program.

Does Golang support reflection?

Reflection is the ability of a program to introspect and analyze its structure during run-time. In Go language, reflection is primarily carried out with types. The reflect package offers all the required APIs/Methods for this purpose.

How do you reflect type in Golang?

TypeOf() Function in Golang is used to get the reflection Type that represents the dynamic type of i. To access this function, one needs to imports the reflect package in the program. Parameters: This function takes only one parameters of interface( i ). Return Value: This function returns the reflection Type.

Why is reflection slow Golang?

Reflection in Go is slow because it needs to do quite a few allocations.


1 Answers

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.

like image 82
nemo Avatar answered Oct 21 '22 06:10

nemo