Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Created March 11, 2015 21:22
Show Gist options
  • Select an option

  • Save alfonsodev/d0edb044141f8e8c59d5 to your computer and use it in GitHub Desktop.

Select an option

Save alfonsodev/d0edb044141f8e8c59d5 to your computer and use it in GitHub Desktop.
go reflection example
package main
import (
"fmt"
"reflect"
)
type User struct {
Name string
Age int
}
func Reflect(u interface{}) {
val := reflect.ValueOf(u).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", typeField.Name, valueField.Interface(), tag.Get("tag_name"))
}
}
func main() {
var b User
b.Name = "a"
Reflect(&b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment