fork download
  1. /*
  2. http://stackoverflow.com/questions/32643815/golang-json-omitempty-with-time-time-field
  3. */
  4.  
  5. package main
  6.  
  7. import (
  8. "encoding/json"
  9. "fmt"
  10. "time"
  11. )
  12.  
  13. type StringStruct struct {
  14. Val string `json:"val,omitempty"`
  15. }
  16.  
  17. type IntStruct struct {
  18. Val int `json:"val,omitempty"`
  19. }
  20. type TimeStruct struct {
  21. Val time.Time `json:"val,omitempty"`
  22. }
  23.  
  24. func main() {
  25. {
  26. a := &StringStruct{}
  27. b, _ := json.Marshal(a)
  28. fmt.Println(string(b))
  29. }
  30. {
  31. a := &IntStruct{}
  32. b, _ := json.Marshal(a)
  33. fmt.Println(string(b))
  34. }
  35. {
  36. a := &TimeStruct{}
  37. b, _ := json.Marshal(a)
  38. fmt.Println(string(b))
  39. }
  40. }
  41.  
Success #stdin #stdout 0s 790016KB
stdin
Standard input is empty
stdout
{}
{}
{"val":"0001-01-01T00:00:00Z"}