如何在Go的text/template中使用.Key?

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

How to use .Key in go text/template usage

问题

我不理解http://golang.org/pkg/text/template/中的文档段落。

  • 数据的键名,必须是一个映射(map),以句点开头,例如
    .Key
    结果是由键名索引的映射元素值。
    键名调用可以链式组合并与任意深度的字段结合使用:
    .Field1.Key1.Field2.Key2
    尽管键名必须是字母数字标识符,但与字段名不同,它们不需要以大写字母开头。
    键名也可以在变量上进行求值,包括链式调用:
    $x.key1.key2

这是我的测试代码,但是失败了。代码:http://play.golang.org/p/lbLJ4yoL2T。

var season = map[int]string{1: "spring", 2: "summer",
        3: "autumn", 4: "winter"}

func main() {
        const greeting = `Welcome, {{.Key}}`
        t := template.Must(template.New("greet").Parse(greeting))
        err := t.Execute(os.Stdout, season)
        if err != nil {
                fmt.Println(err)
        }
}

输出:

Welcome, template: greet:1:11: 在执行"greet"时无法评估类型为map[int]string的字段Key
英文:

I don't understand the document paragraph in http://golang.org/pkg/text/template/

- 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

Here is my test code, but failed. code: http://play.golang.org/p/lbLJ4yoL2T.

var season = map[int]string{1: "spring", 2: "summer",              
        3: "autumn", 4: "winter"}                                  
                                                                   
func main() {                                                      
        const greeting = `Welcome, {{.Key}}`                       
        t := template.Must(template.New("greet").Parse(greeting))  
        err := t.Execute(os.Stdout, season)                        
        if err != nil {                                            
                fmt.Println(err)                                   
        }                                                          
}                                                                  

Output

Welcome, template: greet:1:11: executing "greet" at <.Key>: can't evaluate field Key in type map[int]string

答案1

得分: 1

我会为你翻译以下内容:

我会假设 "Key" 是映射中的键(键/值对中的键)。此外,映射的键不能是 int 类型,无法像那样在模板中使用 {{.Key}}。所以,请尝试使用 {{.a}},就像在你的 playground 的这个分支中所示:

var season = map[string]string{"a": "spring", "b": "summer",
        "c": "autumn", "d": "winter"}

func main() {
        const greeting = `Welcome, {{.a}}`
        t := template.Must(template.New("greet").Parse(greeting))
        err := t.Execute(os.Stdout, season)
        if err != nil {
                fmt.Println(err)
        }
}

输出:

Welcome, spring
英文:

I'd assume "Key" is the key (as in key/value) of the map. Also, the map keys can't be an int to use it in the template like that. So instead of {{.Key}} try {{.a}} as shown in this fork of your playground:

var season = map[string]string{"a": "spring", "b": "summer",              
        "c": "autumn", "d": "winter"}                                  
                                                                   
func main() {                                                      
        const greeting = `Welcome, {{.a}}`                       
        t := template.Must(template.New("greet").Parse(greeting))  
        err := t.Execute(os.Stdout, season)                        
        if err != nil {                                            
                fmt.Println(err)                                   
        }                                                          
}                                                                  

Output:

Welcome, spring

huangapple
  • 本文由 发表于 2014年9月9日 13:20:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/25737152.html
匿名

发表评论

匿名网友

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

确定