第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在沒有解析錯誤的情況下在 Google go 中添加時間

無法在沒有解析錯誤的情況下在 Google go 中添加時間

Go
墨色風雨 2023-06-01 15:07:53
作品:{{ $temp := timestampToDate $var.date }} {{ $temp.Format 2006/01/02 }}不起作用{{ $temp := timestampToDate $var.date }} {{ $temp := $temp.AddDate(0,-1,0) }}     {{ $temp.Format 2006/01/02 }}它說它無法用第二行解析文件,但問題是什么?據(jù)我所知,我正在正確使用命令。
查看完整描述

1 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

乍一看,問題似乎是由于對:=已存在的變量使用語法,但這不是問題,如本例所示:

t?:=?template.Must(template.New("").Parse(`{{$temp?:=?"aa"}}{{$temp}}
{{$temp?:=?"bb"}}{{$temp}}`))

fmt.Println(t.Execute(os.Stdout,?nil))

哪些輸出(在Go Playground上嘗試):

aa
bb<nil>

但是當然,如果變量已經存在,你應該使用賦值=,因為這:=將創(chuàng)建一個新變量,如果發(fā)生在另一個塊內(例如{{range}}or?{{if}}),更改的值將不會保留在塊外。

真正的問題是函數(shù)調用語法:

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

在 Go 模板中你不能使用普通的調用語法,你只需要枚舉參數(shù),用空格分隔,例如:

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

返回的錯誤Template.Execute()表明:

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

這在以下位置有詳細說明template/Pipelines

命令是一個簡單的值(參數(shù))或一個函數(shù)或方法調用,可能帶有多個參數(shù):

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 &#43;0000 UTC m=&#43;0.000000001


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


查看完整回答
反對 回復 2023-06-01
  • 1 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號