Go自定义模板函数的参数

huangapple go评论85阅读模式
英文:

Parameter to Go custom template function

问题

请看 https://play.golang.org/p/EbWA15toVa9

我在其中:

  • 在第25行定义了 func iterate(count int) []int
  • 在第15行调用了 iterate 5

这一切都没问题,但是,当我在第20行调用 iterate (printf "%d" 3) 时,我得到了以下错误:

template: t:12:39: 执行“t”时出错 <3>: 错误的值类型;期望 int,得到 string

难道不应该先执行 (printf "%d" 3),然后在传递给 iterate 时变成 iterate 3 吗?为什么会出现上述错误?

完整的代码如下:

package main

import (
	"log"
	"os"
	"text/template"
)

var x = `{{define "t1"}}
{{- index . 0}} {{index . 1}} {{index . 2}} {{index . 3}}
{{end -}}

hello, {{template "t1" args . 543 false 0.1234}}

{{- range $val := iterate 5 }}
  {{ $val }}
{{- end }}
{{ (printf "%d" 3) }}

{{- range $val := iterate (printf "%d" 3) }}
  {{ $val }}
{{- end }}
`

func iterate(count int) []int {
	var i int
	var Items []int
	for i = 0; i < (count); i++ {
		Items = append(Items, i)
	}
	return Items
}

func args(vs ...interface{}) []interface{} { return vs }

func main() {
	t := template.Must(template.New("t").Funcs(template.FuncMap{"args": args, "iterate": iterate}).Parse(x))
	err := t.Execute(os.Stdout, "foobar")
	if err != nil {
		log.Fatal(err)
	}
}
英文:

Please take a look at https://play.golang.org/p/EbWA15toVa9

in which I

  • define func iterate(count int) []int on line 25
  • call it with iterate 5 on line 15

That's all OK, but, when call it with iterate (printf &quot;%d&quot; 3) on line 20, I got the error of:

template: t:12:39: executing &quot;t&quot; at &lt;3&gt;: wrong type for value; expected int; got string

Wouldn't the (printf &quot;%d&quot; 3) get executed first and when passed to iterate, it'll become iterate 3? Why the above error instead?

Full code enclosed:

package main

import (
	&quot;log&quot;
	&quot;os&quot;
	&quot;text/template&quot;
)

var x = `{{define &quot;t1&quot;}}
{{- index . 0}} {{index . 1}} {{index . 2}} {{index . 3}}
{{end -}}

hello, {{template &quot;t1&quot; args . 543 false 0.1234}}

{{- range $val := iterate 5 }}
  {{ $val }}
{{- end }}
{{ (printf &quot;%d&quot; 3) }}

{{- range $val := iterate (printf &quot;%d&quot; 3) }}
  {{ $val }}
{{- end }}
`

func iterate(count int) []int {
	var i int
	var Items []int
	for i = 0; i &lt; (count); i++ {
		Items = append(Items, i)
	}
	return Items
}

func args(vs ...interface{}) []interface{} { return vs }

func main() {
	t := template.Must(template.New(&quot;t&quot;).Funcs(template.FuncMap{&quot;args&quot;: args, &quot;iterate&quot;: iterate}).Parse(x))
	err := t.Execute(os.Stdout, &quot;foobar&quot;)
	if err != nil {
		log.Fatal(err)
	}
}

答案1

得分: 3

你是正确的,printf "%d" 3会首先被评估。问题似乎是printf "%d" 3生成了一个字符串,但是你的iterate函数的参数类型是int。这个字符串不会被转换。

英文:

You are correct that the printf “%d” 3 gets evaluated first. The problem seems to be that printf “%d” 3 produces a string, but your iterate function has an argument of type int. The string will not be converted.

huangapple
  • 本文由 发表于 2021年9月26日 11:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/69331558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定