无法在 Google 中添加时间到日期而没有解析错误
Can't add time to date in Google go without parse error
作品:
{{ $temp := timestampToDate $var.date }}
{{ $temp.Format 2006/01/02 }}
不起作用
{{ $temp := timestampToDate $var.date }}
{{ $temp := $temp.AddDate(0,-1,0) }}
{{ $temp.Format 2006/01/02 }}
它说它无法解析第二行的文件,但这是什么问题?据我所知,我正在正确使用命令。
乍一看,问题似乎是由于对已存在的变量使用 :=
语法,但这不是问题,如本例所示:
t := template.Must(template.New("").Parse(`{{$temp := "aa"}}{{$temp}}
{{$temp := "bb"}}{{$temp}}`))
fmt.Println(t.Execute(os.Stdout, nil))
哪些输出(在 Go Playground 上尝试):
aa
bb<nil>
当然,如果变量已经存在,你应该使用 =
赋值,因为 :=
将创建一个新变量,如果发生在另一个块中(例如 {{range}}
或 {{if}}
),更改后的值将不会保留在块外。
真正的问题是函数调用语法:
{{ $temp := $temp.AddDate(0,-1,0) }}
在 Go 模板中你不能使用普通的调用语法,你只需要枚举参数,白色-space 分隔,例如:
{{ $temp = $temp.AddDate 0 -1 0 }}
Template.Execute()
返回的错误表明:
panic: template: :3: unexpected "(" in operand
详情见 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.
示例:
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))
输出(在 Go Playground 上尝试):
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
2009-10-10 23:00:00 +0000 UTC<nil>
作品:
{{ $temp := timestampToDate $var.date }}
{{ $temp.Format 2006/01/02 }}
不起作用
{{ $temp := timestampToDate $var.date }}
{{ $temp := $temp.AddDate(0,-1,0) }}
{{ $temp.Format 2006/01/02 }}
它说它无法解析第二行的文件,但这是什么问题?据我所知,我正在正确使用命令。
乍一看,问题似乎是由于对已存在的变量使用 :=
语法,但这不是问题,如本例所示:
t := template.Must(template.New("").Parse(`{{$temp := "aa"}}{{$temp}}
{{$temp := "bb"}}{{$temp}}`))
fmt.Println(t.Execute(os.Stdout, nil))
哪些输出(在 Go Playground 上尝试):
aa
bb<nil>
当然,如果变量已经存在,你应该使用 =
赋值,因为 :=
将创建一个新变量,如果发生在另一个块中(例如 {{range}}
或 {{if}}
),更改后的值将不会保留在块外。
真正的问题是函数调用语法:
{{ $temp := $temp.AddDate(0,-1,0) }}
在 Go 模板中你不能使用普通的调用语法,你只需要枚举参数,白色-space 分隔,例如:
{{ $temp = $temp.AddDate 0 -1 0 }}
Template.Execute()
返回的错误表明:
panic: template: :3: unexpected "(" in operand
详情见 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.
示例:
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))
输出(在 Go Playground 上尝试):
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
2009-10-10 23:00:00 +0000 UTC<nil>