gpt4 book ai didi

go-templates - 比较模板中的字符串

转载 作者:IT老高 更新时间:2023-10-28 13:04:06 25 4
gpt4 key购买 nike

我有以下模板:

{{if . eq "login failed"}}
<span class="text-error">Incorrect username or password</span>
{{else if . eq "login success"}}
<span class="text-success">You have successfully logged in!</span>
{{end}}

我在执行模板时传递了一个字符串。

但是,我收到以下错误:

executing "login.html" at <.>: can't give argument to non-function .

如何比较模板中的字符串?

最佳答案

eq is function ,而不是运算符。它的调用形式为:eq <x> <y> (不是 <x> eq <y> )。

您可以通过移动 eq 两侧的操作数来修复模板在它之后:

{{if eq . "login failed"}}
<span class="text-error">Incorrect username or password</span>
{{else if eq . "login success"}}
<span class="text-success">You have successfully logged in!</span>
{{end}}

关于go-templates - 比较模板中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com