英文:
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
结构体。
func (h virtualAccountsServiceHandler) Get(c echo.Context) (err error) {
virtualAccountID := c.Param("id")
virtualAccount, err := h.service.GetByID(c.Request().Context(), virtualAccountID)
if err != nil {
return
}
return c.JSON(http.StatusOK, virtualAccount)
}
这是结构体的定义:
type VirtualAccount struct {
ID string `json:"id"`
ExpirationDate time.Time `json:"expirationDate"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `json:"-"`
}
如果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
func (h virtualAccountsServiceHandler) Get(c echo.Context) (err error) {
virtualAccountID := c.Param("id")
virtualAccount, err := h.service.GetByID(c.Request().Context(), virtualAccountID)
if err != nil {
return
}
return c.JSON(http.StatusOK, virtualAccount)}
And this is the struct
type VirtualAccount struct {
ID string `json:"id"`
ExpirationDate time.Time `json:"expirationDate"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
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
我测试了这段代码:
package main
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Account struct {
ExpirationDate time.Time `json:"expirationDate"`
}
func hello(c echo.Context) error {
acc := Account{}
return c.JSON(http.StatusOK, acc)
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", hello)
e.Logger.Fatal(e.Start(":1323"))
}
它的执行结果如下:
$ go run main.go
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.7.2
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:1323
{"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}
然而,它返回了你期望的结果:
$ curl localhost:1323
{"expirationDate":"0001-01-01T00:00:00Z"}
英文:
I tested this code:
package main
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Account struct {
ExpirationDate time.Time `json:"expirationDate"`
}
func hello(c echo.Context) error {
acc := Account{}
return c.JSON(http.StatusOK, acc)
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", hello)
e.Logger.Fatal(e.Start(":1323"))
}
Its execution went like this:
$ go run main.go
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.7.2
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:1323
{"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:
$ curl localhost:1323
{"expirationDate":"0001-01-01T00:00:00Z"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论