dongtanjian9310 2015-01-14 09:15
浏览 42
已采纳

如何在Go的html / template中获取地图元素的struct字段?

I have a Struct Task:

type Task struct {
   cmd string
   args []string
   desc string
}

And I init a map which take the above Task struct as a value and a string as a key(the task name)

var taskMap = map[string]Task{
    "find": Task{
        cmd: "find",
        args: []string{"/tmp/"},
        desc: "find files in /tmp dir",
    },
    "grep": Task{
        cmd: "grep",
        args:[]string{"foo","/tmp/*", "-R"},
        desc: "grep files match having foo",
    },
}

and I want to parse a html page using html/template just using the above taskMap.

func listHandle(w http.ResponseWriter, r *http.Request){
    t, _ := template.ParseFiles("index.tmpl")
    t.Execute(w, taskMap)
}

Here is the index.tmpl:

<html>
{{range $key, $value := .}}
   <li>Task Name:        {{$key}}</li>
   <li>Task Value:       {{$value}}</li>
   <li>Task description: {{$value.desc}}</li>
{{end}}
</html>

I can get the $key and value printed successfully, but When It comes to the field of Task using {{$value.desc}} it wont work.

How can I get the desc of each task in this case?

  • 写回答

1条回答 默认 最新

  • duanjucong3124 2015-01-14 10:01
    关注

    Note: you can try/check out your working modified code in the Go Playground.


    If you want the template package to be able to access the fields, you have to export the fields. You can export a field by starting it with an uppercase letter:

    type Task struct {
       cmd string
       args []string
       Desc string
    }
    

    Note that I only changed Desc here, you have to uppercase any other fields you want to refer to in the template.

    After this exporting, change all references to uppercase Desc of course:

    var taskMap = map[string]Task{
        "find": Task{
            cmd: "find",
            args: []string{"/tmp/"},
            Desc: "find files in /tmp dir",
        },
        "grep": Task{
            cmd: "grep",
            args:[]string{"foo","/tmp/*", "-R"},
            Desc: "grep files match having foo",
        },
    }
    

    And also in the template:

    <html>
    {{range $key, $value := .}}
       <li>Task Name:        {{$key}}</li>
       <li>Task Value:       {{$value}}</li>
       <li>Task description: {{$value.Desc}}</li>
    {{end}}
    </html>
    

    Output:

    <html>
    
    <li>Task Name:        find</li>
    <li>Task Value:       {find [/tmp/] find files in /tmp dir}</li>
    <li>Task description: find files in /tmp dir</li>
    
    <li>Task Name:        grep</li>
    <li>Task Value:       {grep [foo /tmp/* -R] grep files match having foo}</li>
    <li>Task description: grep files match having foo</li>
    
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化