大佬教程收集整理的这篇文章主要介绍了golang的json的时间格式化解决方案,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1.通过数据查询的时候指定号格式,这样就不要再转格式了
2.通过time.Time类型穿件别名,然后为这个别名类实现MarshalJSON方法。
// jsontest project main.go
package main
import (
"encoding/json"
"fmt"
"time"
)
type jsonTime time.Time
//实现它的json序列化方法
func (this jsonTimE) MarshalJSON() ([]byte,error) {
var stamp = fmt.Sprintf("\"%s\"",time.Time(this).Format("2006-01-02 15:04:05"))
return []byte(stamp),nil
}
type Test struct {
Date jsonTime `json:"date"`
Name String `json:"name"`
State bool `json:"state"`
}
func main() {
var t = Test{}
t.Date = jsonTime(time.Now())
t.Name = "Hello World"
t.State = true
body,_ := json.Marshal(t)
fmt.Println(String(body))
}
{
"date":"2016-11-04 13:42:24","name":"Hello World","state":true }
以上是大佬教程为你收集整理的golang的json的时间格式化解决方案全部内容,希望文章能够帮你解决golang的json的时间格式化解决方案所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。