Skip to content

Instantly share code, notes, and snippets.

@jtbonhomme
Last active February 27, 2018 09:33
Show Gist options
  • Save jtbonhomme/bd3c5274ce4d2b82ad7e4512775b4ecb to your computer and use it in GitHub Desktop.
Save jtbonhomme/bd3c5274ce4d2b82ad7e4512775b4ecb to your computer and use it in GitHub Desktop.
Gist to display a struct in Go (https://play.golang.org/p/kZy25h7qsq9)
package main
import (
"reflect"
"fmt"
"time"
)
type MyStruct struct {
CreatorId string `json:"creator_id"`
OrganizationId string `json:"organization_id"`
Title string `json:"title"`
Description string `json:"description"`
DateSubmitted time.Time `json:"date_submitted"`
}
func main() {
now := time.Now()
t := MyStruct{"1234", "Organization", "Title", "Description", now}
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())
}
}
@jtbonhomme
Copy link
Author

Result is:

0: CreatorId string = 1234
1: OrganizationId string = Organization
2: Title string = Title
3: Description string = Description
4: DateSubmitted time.Time = 2009-11-10 23:00:00 +0000 UTC m=+0.000000001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment