Skip to content

Instantly share code, notes, and snippets.

@dkarbayev
Created December 20, 2018 13:27
Show Gist options
  • Save dkarbayev/8429cf26c804baa0119bf66af6b41485 to your computer and use it in GitHub Desktop.
Save dkarbayev/8429cf26c804baa0119bf66af6b41485 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
type MyType struct {
Name string
Age int
Height float32
Alive bool
}
func main() {
x := MyType{
"Dan",
29,
177.5,
true,
}
t := reflect.TypeOf(x)
v := reflect.ValueOf(x)
for i := 0; i < t.NumField(); i++ {
n := t.Field(i).Name
fmt.Println(n, "=", v.FieldByName(n).Interface())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment