dtwxt88240 2019-07-12 18:14
浏览 61
已采纳

在Go模板中验证日期

I am trying to ensure I have a valid date in my template file and if so populate a div, otherwise leave it blank. The data type is mysql.NullTime.

Here is what I am trying to do:

{{ if .StartDate ne nil }}
   <div class="box date-row" id="startdate-{{ .DepartureTimeID }}">{{ .StartDate.Format "2006-01-02" }}</div>
{{ else }}
   <div class="box date-row" id="startdate-{{ .DepartureTimeID }}"></div>
{{ end }}

That does seem to work, how can I test for a non-null date?

  • 写回答

1条回答 默认 最新

  • dongluanjie8678 2019-07-13 06:21
    关注

    If it is a mandatory value, you should validate it before you render the template.

    However, if it is optional and/or you are writing a template driven application, you have at least two options to achieve what you want.

    Using zero values only

    Put zero values to good use: for time.Time that is epoch. So, assuming you can not have a StartDate in the past, you can compare wether your StartDate is after epoch.

    package main
    
    import (
        "html/template"
        "os"
        "time"
    )
    
    // Note the call to the `After` function of the date.
    const templateText = `
    {{ if .Data.StartDate.After .Epoch }}
       <div class="box date-row" id="startdate-{{ .Data.DepartureTimeID }}">{{ .Data.StartDate.Format "2006-01-02" }}</div>
    {{ else }}
       <div class="box date-row" id="startdate-{{ .Data.DepartureTimeID }}">No date</div>
    {{ end }}
    `
    
    func main() {
    
         // shortcut for the sake of brevity.
        tmpl := template.Must(template.New("titleTest").Parse(templateText))
    
        // Create an anonymous wrapper struct for your data and the additional
        // time value you need to compare against
        tcx := struct {
    
            // This of course may be of the type you actually use.
            Data struct {
                StartDate       time.Time
                DepartureTimeID int
            }
            Epoch time.Time
        }{
            Data: struct {
                StartDate       time.Time
                DepartureTimeID int
            }{time.Now(), 1},
            Epoch: time.Time{},
        }
    
        tmpl.Execute(os.Stdout, tcx)
    }
    

    <kbd>Run on playground</kbd>

    Using a custom function

    This is pretty much self explanatory: simply define a custom function that validates your date. In this example, I have checked against the zero value again. However, you can get as granular as you wish, of course:

    package main
    
    import (
        "html/template"
        "os"
        "log"
        "time"
    )
    
    const templateText = `
    {{ if afterEpoch .StartDate }}
       <div class="box date-row" id="startdate-{{ .DepartureTimeID }}">{{ .StartDate.Format "2006-01-02" }}</div>
    {{ else }}
       <div class="box date-row" id="startdate-{{ .DepartureTimeID }}"></div>
    {{ end }}
    `
    
    func AfterEpoch(t time.Time) bool {
        return t.After(time.Time{})
    }
    
    type yourData struct {
        DepartureTimeID int
        StartDate       time.Time
    }
    
    func main() {
        funcMap := template.FuncMap{
            "afterEpoch": AfterEpoch,
        }
    
        tmpl := template.Must(template.New("fmap").Funcs(funcMap).Parse(templateText))
    
        log.Println("First run")
        tmpl.Execute(os.Stdout, yourData{1, time.Now()})
    
    
        log.Println("Second run")
        tmpl.Execute(os.Stdout, yourData{DepartureTimeID:1})
    }
    

    Edit:

    Of course, you can also use the pipe notation for the second solution, which is by for more readable, imho: {{ if .StartDate | afterEpoch }}

    <kbd>Run on playground</kbd>

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部