打印时间时出现意外输出。时间类型别名

我正在尝试为自定义类型编写解组函数。考虑以下代码(游乐场

package main


import (

    "encoding/json"

    "fmt"

    "strings"

    "time"

)


type Time time.Time


func (st *Time) UnmarshalJSON(b []byte) error {

    // "2021-05-21T03:10:20.958450" -> "2021-05-21T03:10:20.958450Z"

    s := strings.Trim(string(b), "\"")

    t, err := time.Parse(time.RFC3339, fmt.Sprintf("%s%s", s, "Z"))

    if err != nil {

        return fmt.Errorf("parse time: %w", err)

    }

    *st = Time(t)

    return nil

}


type User struct {

    Name string

    TS Time

}


const data = `{"id":3, "name":"Name", "ts":"2021-05-21T03:10:20.958450"}`


func main() {

    user := new(User)

    json.Unmarshal([]byte(data), &user)

    fmt.Printf("%v\n", user)

}

我成功地time.Time从我那里得到了一个有效的价值,time.Parse()但我不太明白为什么要*st = Time(t)给出如此奇怪的价值?


目前上面打印出来:


&{Name {958450000 63757163420 <nil>}}


但我想更类似于:


&{Name 2021-05-21 03:10:20.95845 +0000 UTC}

我在这里误解了什么?


萧十郎
浏览 82回答 1
1回答

隔江千里

与 time.Time 相反,你的类型没有实现fmt.Stringer,所以 fmt.Print* 函数别无选择,只能使用它们的默认格式化逻辑,在这种情况下是打印底层 time.Time 值的字段大括号。将委托给 time.Time.String 的 String 方法添加到您的类型以获得所需的行为:func (t Time) String() string {&nbsp; &nbsp; return time.Time(t).String()}https://go.dev/play/p/5PwOwa49B5X或者,将您的时间类型更改为嵌入 time.Time。这将自动提升 String 方法以及所有其他方法(例如 Marshal* 方法):type Time struct {&nbsp; &nbsp; time.Time}func (st *Time) UnmarshalJSON(b []byte) error {&nbsp; &nbsp; // ...&nbsp; &nbsp; st.Time = t // simple assignment without type conversion&nbsp; &nbsp; return nil}https://go.dev/play/p/0H5qyCO22gu此外,您永远不应该手动解析 JSON。strings.Trim(string(b), "\"")不足以完全解码 JSON 字符串值。始终使用 json.Unmarshal。您可以使用 time.ParseInLocation 来简化。func (st *Time) UnmarshalJSON(b []byte) error {&nbsp; &nbsp; var s string&nbsp; &nbsp; if err := json.Unmarshal(b, &s); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; t, err := time.ParseInLocation("2006-01-02T15:04:05", s, time.UTC)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; // ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go