doulangbizhan5160 2015-11-11 14:22 采纳率: 100%
浏览 224
已采纳

如何在Golang的Google数据存储区中忽略struct中的零值?

I am trying to use Google Datastore to store data by Go. Since the EndDate is optional field, and don't want to store zero value in that field. If I make a pointer for time field, Google Datastore will send an error message - datastore: unsupported struct field type: *time.Time

How can I ignore zero value field in struct?

type Event struct {
    StartDate time.Time `datastore:"start_date,noindex" json:"startDate"`
    EndDate   time.Time `datastore:"end_date,noindex" json:"endDate"`
}
  • 写回答

2条回答 默认 最新

  • dongshan4316 2015-11-11 19:35
    关注

    The default saving mechanism does not handle optional fields. A field is either saved all the time, or never. There is no such thing as "only save if it's value does not equal to something".

    The "optionally saved property" is considered a custom behavior, a custom saving mechanism, and as such, it has to be implemented manually. Go's way to do this is to implement the PropertyLoadSaver interface on your struct. Here I present 2 different methods to achieve that:

    Manually saving fields

    Here is an example how to do it by manually saving the fields (and excluding EndDate if it is the zero value):

    type Event struct {
        StartDate time.Time `datastore:"start_date,noindex" json:"startDate"`
        EndDate   time.Time `datastore:"end_date,noindex" json:"endDate"`
    }
    
    func (e *Event) Save(c chan<- datastore.Property) error {
        defer close(c)
        // Always save StartDate:
        c <- datastore.Property{Name:"start_date", Value:e.StartDate, NoIndex: true}
    
        // Only save EndDate if not zero value:
        if !e.EndDate.IsZero() {
            c <- datastore.Property{Name:"end_date", Value:e.EndDate, NoIndex: true}
        }
        return nil
    }
    
    func (e *Event) Load(c chan<- datastore.Property) error {
        // No change required in loading, call default implementation:
        return datastore.LoadStruct(e, c)
    }
    

    With another struct

    Here's another way using another struct. The Load() implementation is always the same, only Save() differs:

    func (e *Event) Save(c chan<- datastore.Property) error {
        if !e.EndDate.IsZero() {
            // If EndDate is not zero, save as usual:
            return datastore.SaveStruct(e, c)
        }
    
        // Else we need a struct without the EndDate field:
        s := struct{ StartDate time.Time `datastore:"start_date,noindex"` }{e.StartDate}
        // Which now can be saved using the default saving mechanism:
        return datastore.SaveStruct(&s, c)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog