Skip to content

Instantly share code, notes, and snippets.

@alexrios
Created August 20, 2020 21:12
Show Gist options
  • Save alexrios/5d3188596ff10de8567bc6f3017096a7 to your computer and use it in GitHub Desktop.
Save alexrios/5d3188596ff10de8567bc6f3017096a7 to your computer and use it in GitHub Desktop.
Formatting json.Marshall() using time.Time
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