Created
August 20, 2020 21:12
-
-
Save alexrios/5d3188596ff10de8567bc6f3017096a7 to your computer and use it in GitHub Desktop.
Formatting json.Marshall() using time.Time
This file contains 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 ( | |
"encoding/json" | |
"fmt" | |
"time" | |
) | |
type JSONTime time.Time | |
func (t JSONTime) MarshalJSON() ([]byte, error) { | |
//do your serializing here | |
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("Mon Jan _2")) | |
return []byte(stamp), nil | |
} | |
type Document struct { | |
Name string | |
Content string | |
Stamp JSONTime | |
Author string | |
} | |
func main() { | |
testDoc := Document{"Meeting Notes", "These are some notes", JSONTime(time.Now()), "Bacon"} | |
jsonBytes, _ := json.Marshal(testDoc) | |
fmt.Println(string(jsonBytes)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment