Last active
April 8, 2016 17:45
-
-
Save stoewer/243053aef85bafdcc20acaf4e26abae2 to your computer and use it in GitHub Desktop.
Inspect a struct in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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