dopr25398 2017-12-01 07:07
浏览 53
已采纳

编组和解组时间。时间,获得不同的值

I am try to update my Go code from 1.8 to 1.9. but, i got some problem:

package simple_struct

import (
    "time"
    "encoding/json"
    "github.com/stretchr/testify/assert"
    "testing"
)

type SimpleStruct struct {
    CreateTime time.Time
    Other      string
}

func NewSimpleStruct() *SimpleStruct {
    return &SimpleStruct{
        CreateTime: time.Now(),
    }
}

func Test(t *testing.T) {
    s1 := NewSimpleStruct()
    data, err := json.Marshal(s1)
    assert.Nil(t, err)
    s2 := &SimpleStruct{}
    err = json.Unmarshal(data, s2)
    assert.Nil(t, err)

    assert.Equal(t, s2, s1)
    // this is not equal
    // output:
    /*

    Diff:
    --- Expected
    +++ Actual
    @@ -1,3 +1,3 @@
    (*simple_struct.SimpleStruct)({
        - CreateTime: (time.Time) 2017-12-01 14:54:53.948875421 +0800 CST,
        + CreateTime: (time.Time) 2017-12-01 14:54:53.948875421 +0800 CST m=+0.001827244,
    */
}

It is working in Golang 1.8, but not in Golang 1.9.

I know that Golang 1.9 has update time.Time, but I do not understand this.

  • 写回答

2条回答 默认 最新

  • duancui19840401 2017-12-01 07:32
    关注

    Probably to do with monotonic time being wiped by marshalling. See the the package docs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么