Templates 如何使用;或;在go模板中使用管道?
如何将“or”运算符与多个比较参数一起使用,或者如何找到一些示例?桌子上似乎没有 官方文件对模板中使用的Templates 如何使用;或;在go模板中使用管道?,templates,go,Templates,Go,如何将“or”运算符与多个比较参数一起使用,或者如何找到一些示例?桌子上似乎没有 官方文件对模板中使用的或、和、eq和neq进行了解释。您可以阅读有关模板函数的内容 需要记住的是,模板中提供的函数是前缀符号()。例如,假设not equal运算符的两个参数1和2不相等,则not equal运算符ne 1 2的计算结果将为true。下面是一个模板的示例,该模板使用在前缀中重写的给定表达式和模板函数 package main import ( "os" "text/template
或
、和
、eq
和neq
进行了解释。您可以阅读有关模板函数的内容
需要记住的是,模板中提供的函数是前缀符号()。例如,假设not equal运算符的两个参数1和2不相等,则not equal运算符ne 1 2
的计算结果将为true。下面是一个模板的示例,该模板使用在前缀中重写的给定表达式和模板函数
package main
import (
"os"
"text/template"
)
type Values struct {
Title, X, Y string
}
func main() {
// Parenthesis are used to illustrate order of operation but could be omitted
const given_template = `
{{ if or (and (eq .X "value") (eq .Y "other")) (and (ne .X "a") (eq .Y "b")) }}
print("hello, {{.Title}}")
{{end}}`
values := []Values{
Values{Title: "first", X: "value", Y: "other"},
Values{Title: "second", X: "not a", Y: "b"},
Values{Title: "neither", X: "Hello", Y: "Gopher"},
}
t := template.Must(template.New("example").Parse(given_template))
for _, value := range values {
err := t.Execute(os.Stdout, &value)
if err != nil {
panic(err)
}
}
}
多个比较参数是什么意思?你是说像
x==1 | | x==2
?
package main
import (
"os"
"text/template"
)
type Values struct {
Title, X, Y string
}
func main() {
// Parenthesis are used to illustrate order of operation but could be omitted
const given_template = `
{{ if or (and (eq .X "value") (eq .Y "other")) (and (ne .X "a") (eq .Y "b")) }}
print("hello, {{.Title}}")
{{end}}`
values := []Values{
Values{Title: "first", X: "value", Y: "other"},
Values{Title: "second", X: "not a", Y: "b"},
Values{Title: "neither", X: "Hello", Y: "Gopher"},
}
t := template.Must(template.New("example").Parse(given_template))
for _, value := range values {
err := t.Execute(os.Stdout, &value)
if err != nil {
panic(err)
}
}
}