场景
数据库为mongodb,驱动为mgo。从库中取数据后以json格式返给调用者。
type MyStruct struct{
Time time.Time
}
Time的MarshalJSON实现是转化为RFC3339格式的时间,
若没有赋值,格式化后为0001-01-01T00:00:00Z, 对调用者极不友好
需求:未赋值则返回null
json:",omitempty" {#json-omitempty}
time.Time是结构体,不存在0值,此路不通
换time.Time为*time.Time {#换-time-time-为-time-time}
指向0值的指针不是空指针,数据库中现有数据肯定还是0001-01-01T00:00:00Z
姿势不优雅
实现json.Marshaler {#实现-json-marshaler}
如果时间是0, 就直接返回null
type CustomTime struct{
time.Time
}
func (t CustomTime) MarshalJSON() ([]byte, error) {
fmt.Println(t.String())
if t.Time.IsZero() {
return []byte("null"), nil
}
return t.Time.MarshalJSON()
}
大功告成
其实并没有。经测试发现,没赋值的变成了null,有正常值的也变成了null
因为bson非json,
mgo解析数据时不会调用json.Unmarshaler,CustomTime不再是time.Time
解决:
实现bson.Getter和bson.Setter
func (t CustomTime) GetBSON() (interface{}, error) {
if t.Time.IsZero() {
return nil, nil
}
return t.Time, nil
}
func (t *CustomTime) SetBSON(raw bson.Raw) error {
var tm time.Time
if err := raw.Unmarshal(&tm); err != nil {
return err
}
t.Time = tm
return nil
}
搜索
标签
        
                                            
                     study
                
                            
                    ab
                
                            
                    amap
                
                            
                    apache
                
                            
                    apahe
                
                            
                    awk
                
                            
                    aws
                
                            
                    bat
                
                            
                    centos
                
                            
                    CFS
                
                            
                    chrome
                
                            
                    cmd
                
                            
                    cnpm
                
                            
                    composer
                
                            
                    consul
                
                            
                    crontab
                
                            
                    css
                
                            
                    curl
                
                            
                    cygwin
                
                            
                    devops
                
                            
                    di
                
                            
                    docker
                
                            
                    docker,docker-compose
                
                            
                    ethereum
                
                            
                    excel
                
                            
                    fiddler
                
                            
                    fluentd
                
                            
                    framework
                
                            
                    front-end
                
                            
                    git
                
                            
                    gitgui
                
                            
                    github
                
                            
                    glide
                
                            
                    go
                
                            
                    golang
                
                            
                    gorm
                
                            
                    grafana
                
                            
                    gzip
                
                            
                    ioc
                
                            
                    item2
                
                            
                    iterm2
                
                            
                    javascript
                
                            
                    jenkins
                
                            
                    jsonp
                
                            
                    kafka
                
                            
                    laradock
                
                            
                    laravel
                
                            
                    larval
                
                            
                    linux
                
                            
                    liunux
                
                            
                    log
                
                            
                    mac
                
                            
                    mac, wi-fi
                
                            
                    macos
                
                            
                    magento
                
                            
                    mariaDB
                
                            
                    minikube
                
                            
                    mongoDB
                
                            
                    msp
                
                            
                    mysql
                
                            
                    netbeans
                
                            
                    nginx
                
                            
                    nodejs
                
                            
                    nohup
                
                            
                    npm
                
                            
                    nsq
                
                            
                    php
                
                            
                    php-fpm
                
                            
                    php7
                
                            
                    phpstorm
                
                            
                    php扩展
                
                            
                    Protobuf
                
                            
                    python
                
                            
                    redis
                
                            
                    scp
                
                            
                    server
                
                            
                    shell
                
                            
                    soap
                
                            
                    socket
                
                            
                    socket5
                
                            
                    sql
                
                            
                    sre
                
                            
                    ssdb
                
                            
                    ssh
                
                            
                    ssl
                
                            
                    study
                
                            
                    sublime
                
                            
                    swift
                
                            
                    system
                
                            
                    td-agent
                
                            
                    uml
                
                            
                    v2ray
                
                            
                    vagrant
                
                            
                    vagrnat
                
                            
                    vim
                
                            
                    vpn
                
                            
                    vue
                
                            
                    vue.js
                
                            
                    webpack
                
                            
                    webrtc
                
                            
                    websocket
                
                            
                    webtatic
                
                            
                    windows
                
                            
                    windows7
                
                            
                    word
                
                            
                    wps
                
                            
                    xdebug
                
                            
                    yarn
                
                            
                    yii2
                
                            
                    yum
                
                            
                    zookeeper
                
                            
                    世界国家
                
                            
                    互联网
                
                            
                    以太坊
                
                            
                    分类
                
                            
                    前端
                
                            
                    小程序
                
                            
                    打印机
                
                            
                    排序算法
                
                            
                    搞笑
                
                            
                    权限
                
                            
                    粤语
                
                            
                    缓存
                
                            
                    网络
                
                            
                    虚拟机
                
                            
                    视频
                
                            
                    设计模式
                
                            
                    项目管理
                
                        
热门文章
    
        
    
友情链接
    
    
    
 
         
        