Skip to content

Instantly share code, notes, and snippets.

@defp
Created November 1, 2013 04:15
Show Gist options
  • Select an option

  • Save defp/7260883 to your computer and use it in GitHub Desktop.

Select an option

Save defp/7260883 to your computer and use it in GitHub Desktop.
reflect.go
package main
import (
"fmt"
"reflect"
)
type Post struct {
Id int `field:"id"`
Title string `field:"title"`
Content string `field:"content"`
}
func main() {
var post Post
post.Id = 1
post.Title = "hello"
post.Content = "helloworld"
t := reflect.TypeOf(post)
for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag
fmt.Println(tag.Get("field"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment