doutingyou2198
2019-01-14 12:59
浏览 33
已采纳

无法在Google Go中添加最新日期而没有解析错误

Works:

{{ $temp := timestampToDate $var.date }}
{{ $temp.Format 2006/01/02 }}

Doesn't work

{{ $temp := timestampToDate $var.date }}
{{ $temp := $temp.AddDate(0,-1,0) }}    
{{ $temp.Format 2006/01/02 }}

It says it can't parse the file with the second line but what's the issue? I'm using the command correctly as far as I can see.

图片转代码服务由CSDN问答提供 功能建议

Works:

  {{$ temp:= timestampToDate $  var.date}} 
 {{$ temp.Format 2006/01/02}} 
   
 
 

不起作用 < pre> {{$ temp:= timestampToDate $ var.date}} {{$ temp:= $ temp.AddDate(0,-1,0)}} {{$ temp.Format 2006 / 01/02}}

它说无法用第二行来解析文件,但是出了什么问题? 据我所知,我正在正确使用该命令。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsgrs26202 2019-01-14 13:14
    已采纳

    At first it may seem the problem is due to using := syntax on an already existing variable, but that is not a problem, as this example illustrates:

    t := template.Must(template.New("").Parse(`{{$temp := "aa"}}{{$temp}}
    {{$temp := "bb"}}{{$temp}}`))
    
    fmt.Println(t.Execute(os.Stdout, nil))
    

    Which outputs (try it on the Go Playground):

    aa
    bb<nil>
    

    But of course, if the variable already exists, you should use the = assignment, because the := will create a new variable, which if happens inside another block (e.g. {{range}} or {{if}}), the changed value will not remain outside the block.

    The real problem is the function calling syntax:

    {{ $temp := $temp.AddDate(0,-1,0) }}
    

    In Go templates you can't use the normal call syntax, you just have to enumerate the arguments, white-space separated, e.g.:

    {{ $temp = $temp.AddDate 0 -1 0 }}
    

    The error returned by Template.Execute() indicates this:

    panic: template: :3: unexpected "(" in operand
    

    This is detailed at template/Pipelines:

    A command is a simple value (argument) or a function or method call, possibly with multiple arguments:

    Argument
         The result is the value of evaluating the argument.
    .Method [Argument...]
         The method can be alone or the last element of a chain but,
         unlike methods in the middle of a chain, it can take arguments.
         The result is the value of calling the method with the
         arguments:
             dot.Method(Argument1, etc.)
    functionName [Argument...]
         The result is the value of calling the function associated
         with the name:
             function(Argument1, etc.)
         Functions and function names are described below.
    

    Example:

    t := template.Must(template.New("").Funcs(template.FuncMap{
        "now": time.Now,
    }).Parse(`{{$temp := now}}
    {{$temp}}
    {{$temp = $temp.AddDate 0 -1 0}}
    {{$temp}}`))
    
    fmt.Println(t.Execute(os.Stdout, nil))
    

    Output (try it on the Go Playground):

    2009-11-10 23:00:00 &#43;0000 UTC m=&#43;0.000000001
    
    2009-10-10 23:00:00 &#43;0000 UTC<nil>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报