Created
November 30, 2022 12:58
-
-
Save mnabila/17cbe300384ace1f5aa4ddce223f95ba to your computer and use it in GitHub Desktop.
sample using obj,MarshalJSON()
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 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