Source From Here
QuestionHow can I print (in the console) the Id, Title, Name, etc. of this struct in Golang?
- type Project struct {
- Id int64 `json:"project_id"`
- Title string `json:"title"`
- Name string `json:"name"`
- Data Data `json:"data"`
- Commits Commits `json:"commits"`
- }
To print the name of the fields in a struct:
- fmt.Printf("%+v\n", yourProject)
The article JSON and Go will give more details on how to retrieve the values from a JSON struct. This Go by example page provides another technique:
- type Response2 struct {
- Page int `json:"page"`
- Fruits []string `json:"fruits"`
- }
- res2D := &Response2{
- Page: 1,
- Fruits: []string{"apple", "peach", "pear"}}
- res2B, _ := json.Marshal(res2D)
- fmt.Println(string(res2B))
If you don't have any instance, then you need to use reflection to display the name of the field of a given struct, as in this example:
- type T struct {
- A int
- B string
- }
- t := T{23, "skidoo"}
- s := reflect.ValueOf(&t).Elem()
- typeOfT := s.Type()
- for i := 0; i < s.NumField(); i++ {
- f := s.Field(i)
- fmt.Printf("%d: %s %s = %v\n", i,
- typeOfT.Field(i).Name, f.Type(), f.Interface())
- }
沒有留言:
張貼留言