Can';t在Google go中添加“时间到日期”时不会出现解析错误

Can';t在Google go中添加“时间到日期”时不会出现解析错误,go,go-templates,Go,Go Templates,作品: 不起作用 {{ $temp := timestampToDate $var.date }} {{ $temp.Format 2006/01/02 }} 它说它不能用第二行解析文件,但有什么问题吗?就我所知,我正在正确地使用该命令。起初,问题似乎是由于对已存在的变量使用:=语法造成的,但这不是问题,如本例所示: {{ $temp := timestampToDate $var.date }} {{ $temp := $temp.AddDate(0,-1,0) }} {{ $tem

作品:

不起作用

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

它说它不能用第二行解析文件,但有什么问题吗?就我所知,我正在正确地使用该命令。

起初,问题似乎是由于对已存在的变量使用
:=
语法造成的,但这不是问题,如本例所示:

{{ $temp := timestampToDate $var.date }}
{{ $temp := $temp.AddDate(0,-1,0) }}    
{{ $temp.Format 2006/01/02 }}
哪些输出(在上尝试):

在Go模板中,您不能使用正常的调用语法,您只需枚举参数,空格分隔,例如:

{{ $temp := $temp.AddDate(0,-1,0) }}
返回的错误表明:

{{ $temp = $temp.AddDate 0 -1 0 }}
详情请参阅:

命令是一个简单的值(参数)或函数或方法调用,可能有多个参数:

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

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.
输出(在上尝试):

2009-11-1023:00:00+;0000 UTC m=&43;0.000000001
2009-10-10 23:00:00 +协调世界时0000

您两次声明了
$temp
:=
)。使用
=
在第二行赋值,重新声明变量为
:=
。看见与时间或日期完全无关。“不起作用”不是问题陈述。你看到什么错误了?太棒了。感谢您对作业以及如何正确调用函数的说明。显然,关于这个语法,我还有很多东西要学。
panic: template: :3: unexpected "(" in operand
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.
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))
2009-11-10 23:00:00 +0000 UTC m=+0.000000001

2009-10-10 23:00:00 &#43;0000 UTC<nil>