You can indeed use reflection to do this. You primarily want reflect.TypeOf
, reflect.Type.Field
, reflect.Type.NumField
, and reflect.StructField
Code:
package main
import "fmt"
import "reflect"
type Person struct {
name string
age int
}
func main() {
typ := reflect.TypeOf(Person{})
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
fmt.Println("Field name:", field.Name)
fmt.Println("Field type:", field.Type)
fmt.Println()
}
}
Some notes:
- This works for both structs in your package and out of your package
- If you need to actually change or read data, use
reflect.ValueOf
and pass it a pointer to the struct, followed by a call toValue.Elem()
- You cannot set unexported fields in another package via reflect without a panic (well, okay, you can, but it involves unsafe and ain't pretty), but you can read them. This is not recommended.
- Always consider if you actually need reflection before you use it. At the current time, you need to have Go source to successfully import the package, so if you just need to know field names it may be better to just poke around the source code. If you need to know at runtime, ask yourself why and if there's any possible way around it. Reflect is a landmine of bugs and quirky behavior.