Skip to content

Instantly share code, notes, and snippets.

@billiepander
Last active August 24, 2017 07:10
Show Gist options
  • Select an option

  • Save billiepander/f2aa6fc5e824f51d98f627767f8d9826 to your computer and use it in GitHub Desktop.

Select an option

Save billiepander/f2aa6fc5e824f51d98f627767f8d9826 to your computer and use it in GitHub Desktop.
useful to convert struct to map use specific tag as key
package main
import (
"fmt"
"reflect"
)
type User struct{
Name string `json:"name" bson:"b_name"`
Age int `json:"age" bson:"b_age"`
}
func main() {
var u User
t:=reflect.TypeOf(u)
for i:=0;i<t.NumField();i++{
sf:=t.Field(i)
fmt.Println(sf.Tag.Get("json"),",",sf.Tag.Get("bson"))
}
}
# 输出
name , b_name
age , b_age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment