Skip to content

Instantly share code, notes, and snippets.

@mnabila
Created November 30, 2022 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnabila/17cbe300384ace1f5aa4ddce223f95ba to your computer and use it in GitHub Desktop.
Save mnabila/17cbe300384ace1f5aa4ddce223f95ba to your computer and use it in GitHub Desktop.
sample using obj,MarshalJSON()
package main
import (
"encoding/json"
"fmt"
"time"
)
type JSONTime struct {
time.Time
}
func (t JSONTime) MarshalJSON() ([]byte, error) {
formated := t.Format("2006/01/02 15:04")
return json.Marshal(formated)
}
type Sample struct {
DateTime JSONTime `json:"date_time,omitempty"`
Name string `json:"name,omitempty"`
}
func main() {
obj := Sample{
DateTime: JSONTime{time.Now()},
Name: "NANA NUNU",
}
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment