Skip to content

Instantly share code, notes, and snippets.

@indraniel
Created January 26, 2015 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indraniel/865818e29fb806c5907e to your computer and use it in GitHub Desktop.
Save indraniel/865818e29fb806c5907e to your computer and use it in GitHub Desktop.
Playing around with golang's reflection package. Based on reading http://blog.golang.org/2011/09/laws-of-reflection.html
package main
import "reflect"
import "fmt"
type T struct {
A int
B float64
C int32
D string
}
func main() {
t := T{23, 2.46, 34, "eggo"}
s := reflect.ValueOf(t)
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
val := fmt.Sprintf("%v", f.Interface())
fmt.Printf("%d: %s %s = %v (%s) \n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface(), val)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment