How to convert a map to html table in Go

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

How to convert a map to html table in Go

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是Go语言的新手,正在练习,你能帮我将一个map转换成HTML表格吗?我有一个函数从一些REST端点获取数据,并返回一个根据端点返回结果大小可能变化的map。

  1. type color struct {
  2. blue int
  3. red int
  4. }
  5. func fetchData() map[string]map[string]colour {}

打印这个函数的输出如下,但每次可能会有更多或更少的列:

  1. map[joe:map[alex: {3 6} may:{2 6}] jena:map[fred: {1 2}]]

我想要一个像这样的HTML表格:

老师 学生 蓝色笔 红色笔
joe alex 3 6
may 2 6
jena fred 1 2
英文:

I'm newbie in Go and practicing, could you please help to convert a map to an html table in GO?
I have a function that fetches some data from some rest end points and return a map that can vary in size based on the result I get back from the endpoints.

  1. type color struct {
  2. blue int
  3. red int
  4. }
  5. func fetchData() map[string]map[string]colour {}

printing out the output of this function looks like this but can vary each time with more or less columns

  1. map[joe:map[alex: {3 6} may:{2 6}] jena:map[fred: {1 2}]]

I would to have a html table like this:

Teacher Student Blue Pens Red Pens
joe alex 3 6
may 2 6
jena fred 1 2

答案1

得分: 5

我认为最简单的方法是使用html/template包。text/template的文档解释了语法,以防您对模板引擎不熟悉。

像这样(playground链接):

  1. package main
  2. import (
  3. "html/template"
  4. "os"
  5. )
  6. const tplStr = `<table>
  7. <thead>
  8. <tr>
  9. <th>Teacher</th>
  10. <th>Student</th>
  11. <th>Blue Pens</th>
  12. <th>Red Pens</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. {{range $teacher, $rows := . }}
  17. {{ $first := true }}
  18. {{ range $student, $colors := . }}
  19. <tr>
  20. <td>{{ if $first }}{{ $first = false }}{{ $teacher }}{{ end }}</td>
  21. <td>{{ $student }}</td>
  22. <td>{{ $colors.Blue }}</td>
  23. <td>{{ $colors.Red }}</td>
  24. </tr>
  25. {{ end }}
  26. {{ end }}
  27. </tbody>
  28. </table>`
  29. type color struct {
  30. Blue int
  31. Red int
  32. }
  33. func fetchData() map[string]map[string]color {
  34. return map[string]map[string]color{
  35. "joe": {
  36. "alex": {
  37. Blue: 3,
  38. Red: 6,
  39. },
  40. "may": {
  41. Blue: 2,
  42. Red: 6,
  43. },
  44. },
  45. "jena": {
  46. "fred": color{
  47. Blue: 1,
  48. Red: 2,
  49. },
  50. },
  51. }
  52. }
  53. func main() {
  54. tpl, err := template.New("table").Parse(tplStr)
  55. if err != nil {
  56. panic(err)
  57. }
  58. err = tpl.Execute(os.Stdout, fetchData())
  59. if err != nil {
  60. panic(err)
  61. }
  62. }
英文:

The easiest way in my opinion is to use the html/template package. The docs of text/template explain the syntax, in case you are not familiar with the templating engine.

Like so (playground link):

  1. package main
  2. import (
  3. &quot;html/template&quot;
  4. &quot;os&quot;
  5. )
  6. const tplStr = `&lt;table&gt;
  7. &lt;thead&gt;
  8. &lt;tr&gt;
  9. &lt;th&gt;Teacher&lt;/th&gt;
  10. &lt;th&gt;Student&lt;/th&gt;
  11. &lt;th&gt;Blue Pens&lt;/th&gt;
  12. &lt;th&gt;Red Pens&lt;/th&gt;
  13. &lt;/tr&gt;
  14. &lt;/thead&gt;
  15. &lt;tbody&gt;
  16. {{range $teacher, $rows := . }}
  17. {{ $first := true }}
  18. {{ range $student, $colors := . }}
  19. &lt;tr&gt;
  20. &lt;td&gt;{{ if $first }}{{ $first = false }}{{ $teacher }}{{ end }}&lt;/td&gt;
  21. &lt;td&gt;{{ $student }}&lt;/td&gt;
  22. &lt;td&gt;{{ $colors.Blue }}&lt;/td&gt;
  23. &lt;td&gt;{{ $colors.Red }}&lt;/td&gt;
  24. &lt;/tr&gt;
  25. {{ end }}
  26. {{ end }}
  27. &lt;/tbody&gt;
  28. &lt;/table&gt;`
  29. type color struct {
  30. Blue int
  31. Red int
  32. }
  33. func fetchData() map[string]map[string]color {
  34. return map[string]map[string]color{
  35. &quot;joe&quot;: {
  36. &quot;alex&quot;: {
  37. Blue: 3,
  38. Red: 6,
  39. },
  40. &quot;may&quot;: {
  41. Blue: 2,
  42. Red: 6,
  43. },
  44. },
  45. &quot;jena&quot;: {
  46. &quot;fred&quot;: color{
  47. Blue: 1,
  48. Red: 2,
  49. },
  50. },
  51. }
  52. }
  53. func main() {
  54. tpl, err := template.New(&quot;table&quot;).Parse(tplStr)
  55. if err != nil {
  56. panic(err)
  57. }
  58. err = tpl.Execute(os.Stdout, fetchData())
  59. if err != nil {
  60. panic(err)
  61. }
  62. }

huangapple
  • 本文由 发表于 2022年1月15日 03:15:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/70715508.html
匿名

发表评论

匿名网友

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

确定