Last active
February 27, 2018 09:33
-
-
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)
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 ( | |
"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()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result is: