Guest User

Untitled

a guest
Jan 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. type ProfileJSON struct {
  2. Name string `json:"name"`
  3. Address string `json:"address"`
  4. CreatedAt int64 `json:"created_at"`
  5. }
  6.  
  7. type Profile struct {
  8. Name string
  9. Address string
  10. Password string
  11. CreatedAt string
  12. }
  13.  
  14. func (p *Profile) MarshalJSON() ([]byte, error) {
  15. millis, err := time.Parse(time.RFC3339, p.CreatedAt)
  16. if err != nil {
  17. return nil, err
  18. }
  19.  
  20. res := ProfileJSON{
  21. Name: p.Name,
  22. Address: p.Address,
  23. CreatedAt: millis.UnixNano() / 1000000,
  24. }
  25.  
  26. return json.Marshal(&res)
  27. }
  28.  
  29. func main() {
  30. profile := Profile{
  31. Name: "name",
  32. Address: "address",
  33. Password: "password",
  34. CreatedAt: "2019-01-22T06:11:43Z",
  35. }
  36.  
  37. res, err := json.Marshal(&profile)
  38. if err != nil {
  39. fmt.Println(err)
  40. }
  41.  
  42. fmt.Println(string(res))
  43. }
Add Comment
Please, Sign In to add comment