Get name of struct field using reflection

Go

Go Problem Overview


What is the way of printing "Foo" here? In this example, what prints is "string".

http://play.golang.org/p/ZnK6PRwEPp

type A struct {
	Foo string
}

func (a *A) PrintFoo() {
	fmt.Println("Foo value is " + a.Foo)
}

func main() {
	a := &A{Foo: "afoo"}
	val := reflect.Indirect(reflect.ValueOf(a))
	fmt.Println(val.Field(0).Type().Name())
}

Go Solutions


Solution 1 - Go

You want val.Type().Field(0).Name. The Field method on reflect.Type will return a struct describing that field, which includes the name, among other information.

There is no way to retrieve the field name for a reflect.Value representing a particular field value, since that is a property of the containing struct.

Solution 2 - Go

You need to Get the Field of the Type Definition not of the Value.

http://play.golang.org/p/7Bc7MJikbJ

package main

import "fmt"
import "reflect"

type A struct {
	Foo string
}

func (a *A) PrintFoo() {
	fmt.Println("Foo value is " + a.Foo)
}

func main() {
	a := &A{Foo: "afoo"}
	val := reflect.Indirect(reflect.ValueOf(a))
	fmt.Println(val.Type().Field(0).Name)
}

Solution 3 - Go

I think the better way to get the fields' name in the struct is

func main() {
    a := &A{Foo: "afoo"}
    val := reflect.ValueOf(a).Elem()
    for i:=0; i<val.NumField();i++{
        fmt.Println(val.Type().Field(i).Name)
    }
}

There are two tips:

  1. use .Elem() after you reflect.ValueOf(a), because in your case, a is a pointer.
  2. val.Field(i).Type().Name is totally different from val.Type().Field(i).Name. The latter one can get the name of the field in the struct

Hope that it is helpful..

If you want to have a look at more cases, please check my 2mins article

Solution 4 - Go

With the new Names method of the structs package it's even more easier:

package main

import (
	"fmt"

	"github.com/fatih/structs"
)

type A struct {
	Foo string
	Bar int
}

func main() {
	names := structs.Names(&A{})
	fmt.Println(names) // ["Foo", "Bar"]
}

Solution 5 - Go

You can also use https://github.com/fatih/structs

// Convert the fields of a struct to a []*Field
fields := s.Fields()

for _, f := range fields {
    fmt.Printf("field name: %+v\n", f.Name())
}

Solution 6 - Go

package main

import "fmt"
import "reflect"

type A struct {
	Foo string
}

func (a *A) PrintFoo() {
	fmt.Println("Foo value is " + a.Foo)
}

func main() {
	a := &A{Foo: "afoo"}
	
	//long and bored code
	t := reflect.TypeOf(*a)
	if t.Kind() == reflect.Struct {
		for i := 0; i < t.NumField(); i++ {
			fmt.Println(t.Field(i).Name)
		}
	} else {
		fmt.Println("not a stuct")
	}

	//shorthanded call
	fmt.Println(reflect.TypeOf(*a).Field(0).Name)//can panic if no field exists

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsatView Question on Stackoverflow
Solution 1 - GoJames HenstridgeView Answer on Stackoverflow
Solution 2 - GofabrizioMView Answer on Stackoverflow
Solution 3 - GooscarzView Answer on Stackoverflow
Solution 4 - GoFatih ArslanView Answer on Stackoverflow
Solution 5 - GoThellimistView Answer on Stackoverflow
Solution 6 - GoViktor BushminView Answer on Stackoverflow