Skip to content

Instantly share code, notes, and snippets.

@stoewer
Last active April 8, 2016 17:45
Show Gist options
  • Save stoewer/243053aef85bafdcc20acaf4e26abae2 to your computer and use it in GitHub Desktop.
Save stoewer/243053aef85bafdcc20acaf4e26abae2 to your computer and use it in GitHub Desktop.
Inspect a struct in go
package main
import (
"fmt"
"reflect"
)
type Foo struct {
Bar string `param:"bar"`
Bla string `param:"bla"`
}
func main() {
foo := Foo{Bar: "bar", Bla: "bla"}
typ := reflect.TypeOf(foo)
fcount := typ.NumField()
for i:= 0; i < fcount; i++ {
field := typ.Field(i)
fmt.Println(field.Name)
fmt.Println(field.Tag.Get("param"))
}
fmt.Println(foo)
fmt.Println(fcount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment