使用 eq 和 index 的 Go 模板
Go templates with eq and index
Go 模板在使用 eq
和 index
时有一些意想不到的结果。请参阅此代码:
package main
import (
"os"
"text/template"
)
func main() {
const myTemplate = `
{{range $n := .}}
{{index $n 0}} {{if (index $n 0) eq (index $n 1)}}={{else}}!={{end}} {{index $n 1}}
{{end}}
`
t := template.Must(template.New("").Parse(myTemplate))
t.Execute(os.Stdout,
[][2]int{
[2]int{1, 2},
[2]int{2, 2},
[2]int{4, 2},
})
}
我希望有输出
1 != 2
2 = 2
4 != 2
但我得到
1 = 2
2 = 2
4 = 2
我应该更改什么才能比较 go 模板中的数组成员?
eq
是前缀运算:
{{if eq (index $n 0) (index $n 1)}}={{else}}!={{end}}
您使用了错误的运算符和参数顺序。你必须先写运算符,然后再写操作数:
{{if eq (index $n 0) (index $n 1)}}
这更具可读性和方便性,因为 eq
可以接受不止 2 个参数,因此您可以这样写:
{{if eq (index $n 0) (index $n 1) (index $n 2)}}
For simpler multi-way equality tests, eq (only) accepts two or more arguments and compares the second and subsequent to the first, returning in effect
arg1==arg2 || arg1==arg3 || arg1==arg4 ...
(Unlike with || in Go, however, eq is a function call and all the arguments will be evaluated.)
使用此更改输出(在 Go Playground 上尝试):
1 != 2
2 = 2
4 != 2
注:
您不需要引入 "loop" 变量,{{range}}
操作将点更改为当前项目:
...dot is set to the successive elements of the array, slice, or map...
所以你可以简化你的模板,这相当于你的:
{{range .}}
{{index . 0}} {{if eq (index . 0) (index . 1)}}={{else}}!={{end}} {{index . 1}}
{{end}}
另请注意,您可以自己在模板中创建变量,如果您多次使用相同的表达式(例如 index . 0
),建议这样做。这也相当于你的模板:
{{range .}}{{[=15=] := index . 0}}{{ := index . 1}}
{{[=15=]}} {{if eq [=15=] }}={{else}}!={{end}} {{}}
{{end}}
另请注意,在这种特定情况下,由于您要在 if
和 else
分支中输出的内容都包含 =
符号,因此您不需要 2 个分支, =
两种情况都需要输出,如果不相等则只需要一个额外的 !
符号即可。所以下面的最终模板也等同于你的:
{{range .}}{{[=16=] := index . 0}}{{ := index . 1}}
{{[=16=]}} {{if ne [=16=] }}!{{end}}= {{}}
{{end}}
Go 模板在使用 eq
和 index
时有一些意想不到的结果。请参阅此代码:
package main
import (
"os"
"text/template"
)
func main() {
const myTemplate = `
{{range $n := .}}
{{index $n 0}} {{if (index $n 0) eq (index $n 1)}}={{else}}!={{end}} {{index $n 1}}
{{end}}
`
t := template.Must(template.New("").Parse(myTemplate))
t.Execute(os.Stdout,
[][2]int{
[2]int{1, 2},
[2]int{2, 2},
[2]int{4, 2},
})
}
我希望有输出
1 != 2
2 = 2
4 != 2
但我得到
1 = 2
2 = 2
4 = 2
我应该更改什么才能比较 go 模板中的数组成员?
eq
是前缀运算:
{{if eq (index $n 0) (index $n 1)}}={{else}}!={{end}}
您使用了错误的运算符和参数顺序。你必须先写运算符,然后再写操作数:
{{if eq (index $n 0) (index $n 1)}}
这更具可读性和方便性,因为 eq
可以接受不止 2 个参数,因此您可以这样写:
{{if eq (index $n 0) (index $n 1) (index $n 2)}}
For simpler multi-way equality tests, eq (only) accepts two or more arguments and compares the second and subsequent to the first, returning in effect
arg1==arg2 || arg1==arg3 || arg1==arg4 ...
(Unlike with || in Go, however, eq is a function call and all the arguments will be evaluated.)
使用此更改输出(在 Go Playground 上尝试):
1 != 2
2 = 2
4 != 2
注:
您不需要引入 "loop" 变量,{{range}}
操作将点更改为当前项目:
...dot is set to the successive elements of the array, slice, or map...
所以你可以简化你的模板,这相当于你的:
{{range .}}
{{index . 0}} {{if eq (index . 0) (index . 1)}}={{else}}!={{end}} {{index . 1}}
{{end}}
另请注意,您可以自己在模板中创建变量,如果您多次使用相同的表达式(例如 index . 0
),建议这样做。这也相当于你的模板:
{{range .}}{{[=15=] := index . 0}}{{ := index . 1}}
{{[=15=]}} {{if eq [=15=] }}={{else}}!={{end}} {{}}
{{end}}
另请注意,在这种特定情况下,由于您要在 if
和 else
分支中输出的内容都包含 =
符号,因此您不需要 2 个分支, =
两种情况都需要输出,如果不相等则只需要一个额外的 !
符号即可。所以下面的最终模板也等同于你的:
{{range .}}{{[=16=] := index . 0}}{{ := index . 1}}
{{[=16=]}} {{if ne [=16=] }}!{{end}}= {{}}
{{end}}