Last active
August 24, 2017 07:10
-
-
Save billiepander/f2aa6fc5e824f51d98f627767f8d9826 to your computer and use it in GitHub Desktop.
useful to convert struct to map use specific tag as key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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