Golang echo.JSON turns Zero time.Time as Empty String but I Expect to be Returned as "0001-01-01 00:00:00"

huangapple go评论103阅读模式
英文:

Golang echo.JSON turns Zero time.Time as Empty String but I Expect to be Returned as "0001-01-01 00:00:00"

问题

你好,我想问一些关于Golang echo.JSON()方法的问题。这行代码将返回一个virtualAccount结构体。

  1. func (h virtualAccountsServiceHandler) Get(c echo.Context) (err error) {
  2. virtualAccountID := c.Param("id")
  3. virtualAccount, err := h.service.GetByID(c.Request().Context(), virtualAccountID)
  4. if err != nil {
  5. return
  6. }
  7. return c.JSON(http.StatusOK, virtualAccount)
  8. }

这是结构体的定义:

  1. type VirtualAccount struct {
  2. ID string `json:"id"`
  3. ExpirationDate time.Time `json:"expirationDate"`
  4. CreatedAt time.Time `json:"createdAt"`
  5. UpdatedAt time.Time `json:"updatedAt"`
  6. DeletedAt time.Time `json:"-"`
  7. }

如果ExpirationDate是一个正常的日期,这个函数会正常返回。但是当数据包含零值的time.Time(如此处所述https://pkg.go.dev/time)时,问题就出现了。

h.service.GetByID方法会返回原始日期(0001-01-01 00:00:00.000),但echo.JSON会返回空字符串"",因为时间数据现在的值是time.Time{}

如何使echo.JSON返回原始日期(0001-01-01 00:00:00.000)呢?

英文:

Hi I want to ask some question regarding Golang echo.JSON() method. This line of code will return a virtualAccount struct

  1. func (h virtualAccountsServiceHandler) Get(c echo.Context) (err error) {
  2. virtualAccountID := c.Param("id")
  3. virtualAccount, err := h.service.GetByID(c.Request().Context(), virtualAccountID)
  4. if err != nil {
  5. return
  6. }
  7. return c.JSON(http.StatusOK, virtualAccount)}

And this is the struct

  1. type VirtualAccount struct {
  2. ID string `json:"id"`
  3. ExpirationDate time.Time `json:"expirationDate"`
  4. CreatedAt time.Time `json:"createdAt"`
  5. UpdatedAt time.Time `json:"updatedAt"`
  6. DeletedAt time.Time `json:"-"`}

This function returns normally if the ExpirationDate is a normal date. But the problem arise when the data contains zero time.Time (0001-01-01 00:00:00.000 as stated here https://pkg.go.dev/time).

h.service.GetByID method will returns the date as it is (0001-01-01 00:00:00.000) but the echo.JSON will return empty string "" because now the time data has value of time.Time{}.

How to make echo.JSON returns it as it is (0001-01-01 00:00:00.000)?

答案1

得分: 1

我测试了这段代码:

  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/labstack/echo/v4"
  6. "github.com/labstack/echo/v4/middleware"
  7. )
  8. type Account struct {
  9. ExpirationDate time.Time `json:"expirationDate"`
  10. }
  11. func hello(c echo.Context) error {
  12. acc := Account{}
  13. return c.JSON(http.StatusOK, acc)
  14. }
  15. func main() {
  16. e := echo.New()
  17. e.Use(middleware.Logger())
  18. e.Use(middleware.Recover())
  19. e.GET("/", hello)
  20. e.Logger.Fatal(e.Start(":1323"))
  21. }

它的执行结果如下:

  1. $ go run main.go
  2. ____ __
  3. / __/___/ / ___
  4. / _// __/ _ \/ _ \
  5. /___/\__/_//_/\___/ v4.7.2
  6. High performance, minimalist Go web framework
  7. https://echo.labstack.com
  8. ____________________________________O/_______
  9. O\
  10. http server started on [::]:1323
  11. {"time":"2022-04-30T17:06:24.090332232-03:00","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/","user_agent":"curl/7.29.0","status":200,"error":"","latency":146878,"latency_human":"146.878µs","bytes_in":0,"bytes_out":42}

然而,它返回了你期望的结果:

  1. $ curl localhost:1323
  2. {"expirationDate":"0001-01-01T00:00:00Z"}
英文:

I tested this code:

  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/labstack/echo/v4"
  6. "github.com/labstack/echo/v4/middleware"
  7. )
  8. type Account struct {
  9. ExpirationDate time.Time `json:"expirationDate"`
  10. }
  11. func hello(c echo.Context) error {
  12. acc := Account{}
  13. return c.JSON(http.StatusOK, acc)
  14. }
  15. func main() {
  16. e := echo.New()
  17. e.Use(middleware.Logger())
  18. e.Use(middleware.Recover())
  19. e.GET("/", hello)
  20. e.Logger.Fatal(e.Start(":1323"))
  21. }

Its execution went like this:

  1. $ go run main.go
  2. ____ __
  3. / __/___/ / ___
  4. / _// __/ _ \/ _ \
  5. /___/\__/_//_/\___/ v4.7.2
  6. High performance, minimalist Go web framework
  7. https://echo.labstack.com
  8. ____________________________________O/_______
  9. O\
  10. http server started on [::]:1323
  11. {"time":"2022-04-30T17:06:24.090332232-03:00","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/","user_agent":"curl/7.29.0","status":200,"error":"","latency":146878,"latency_human":"146.878µs","bytes_in":0,"bytes_out":42}

However it returned what you expected:

  1. $ curl localhost:1323
  2. {"expirationDate":"0001-01-01T00:00:00Z"}

huangapple
  • 本文由 发表于 2022年5月1日 00:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/72070338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定