亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

匿名結構作為模板中的管道

匿名結構作為模板中的管道

Go
鳳凰求蠱 2023-06-12 17:09:53
有沒有辦法在 a 中執行以下操作html/template?{{template "mytemplate" struct{Foo1, Foo2 string}{"Bar1", "Bar2"}}}實際上在模板中,就像上面一樣。不是通過注冊的函數FuncMap返回結構。我試過了,但Parse很恐慌,請參閱 Playground。也許只是語法錯誤?
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

正如其他人所指出的,這是不可能的。模板在運行時解析,無需 Go 編譯器的幫助。因此,允許任意 Go 語法是不可行的(盡管請注意,這并非不可能,因為標準庫包含解析 Go 源文本的所有工具,請參閱標準庫中“前綴為”的包)go/。根據設計理念,復雜的邏輯應該在模板之外。

回到你的例子:

struct{Foo1,?Foo2?string}{"Bar1",?"Bar2"}

這是一個結構復合文字,它在模板中不受支持,無論是在調用另一個模板時還是在其他地方。

使用自定義“參數”調用另一個模板具有以下語法(引用自text/template:Actions):

{{template?"name"?pipeline}}
????The?template?with?the?specified?name?is?executed?with?dot?set
????to?the?value?of?the?pipeline.

長話短說;博士;管道可能是一個常量,一個表示某個字段或方法的表達式(方法將被調用并使用其返回值),它可能是對某些“模板內置”函數的調用或自定義注冊函數或映射中的值。

管道在哪里:

管道可能是“命令”的鏈式序列。命令是一個簡單的值(參數)或一個函數或方法調用,可能帶有多個參數:

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.


一個論點是:

參數是一個簡單的值,由以下之一表示。

-?A?boolean,?string,?character,?integer,?floating-point,?imaginary
??or?complex?constant?in?Go?syntax.?These?behave?like?Go's?untyped
??constants.?Note?that,?as?in?Go,?whether?a?large?integer?constant
??overflows?when?assigned?or?passed?to?a?function?can?depend?on?whether
??the?host?machine's?ints?are?32?or?64?bits.
-?The?keyword?nil,?representing?an?untyped?Go?nil.
-?The?character?'.'?(period):
??.
??The?result?is?the?value?of?dot.
-?A?variable?name,?which?is?a?(possibly?empty)?alphanumeric?string
??preceded?by?a?dollar?sign,?such?as
??$piOver2
??or
??$
??The?result?is?the?value?of?the?variable.
??Variables?are?described?below.
-?The?name?of?a?field?of?the?data,?which?must?be?a?struct,?preceded
??by?a?period,?such?as
??.Field
??The?result?is?the?value?of?the?field.?Field?invocations?may?be
??chained:
????.Field1.Field2
??Fields?can?also?be?evaluated?on?variables,?including?chaining:
????$x.Field1.Field2
-?The?name?of?a?key?of?the?data,?which?must?be?a?map,?preceded
??by?a?period,?such?as
??.Key
??The?result?is?the?map?element?value?indexed?by?the?key.
??Key?invocations?may?be?chained?and?combined?with?fields?to?any
??depth:
????.Field1.Key1.Field2.Key2
??Although?the?key?must?be?an?alphanumeric?identifier,?unlike?with
??field?names?they?do?not?need?to?start?with?an?upper?case?letter.
??Keys?can?also?be?evaluated?on?variables,?including?chaining:
????$x.key1.key2
-?The?name?of?a?niladic?method?of?the?data,?preceded?by?a?period,
??such?as
??.Method
??The?result?is?the?value?of?invoking?the?method?with?dot?as?the
??receiver,?dot.Method().?Such?a?method?must?have?one?return?value?(of
??any?type)?or?two?return?values,?the?second?of?which?is?an?error.
??If?it?has?two?and?the?returned?error?is?non-nil,?execution?terminates
??and?an?error?is?returned?to?the?caller?as?the?value?of?Execute.
??Method?invocations?may?be?chained?and?combined?with?fields?and?keys
??to?any?depth:
????.Field1.Key1.Method1.Field2.Key2.Method2
??Methods?can?also?be?evaluated?on?variables,?including?chaining:
????$x.Method1.Field
-?The?name?of?a?niladic?function,?such?as
??fun
??The?result?is?the?value?of?invoking?the?function,?fun().?The?return
??types?and?values?behave?as?in?methods.?Functions?and?function
??names?are?described?below.
-?A?parenthesized?instance?of?one?the?above,?for?grouping.?The?result
??may?be?accessed?by?a?field?or?map?key?invocation.??print?(.F1?arg1)?(.F2?arg2)
??(.StructValuedMethod?"arg").Field

正確的解決方案是注冊一個自定義函數,該函數構造要傳遞給模板調用的值,

另一個半解決方案可能是使用內置函數printprintf函數來連接您要傳遞的值,但這需要在另一個模板中拆分。


查看完整回答
反對 回復 2023-06-12
?
PIPIONE

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

這是不可能的。

但是,您可能希望為模板提供一個通用dict函數,以允許map[string]interface{}從參數列表構建一個。

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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

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

公眾號

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