How to convert a map to html table in Go

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

How to convert a map to html table in Go

问题

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

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

type color struct {
   blue int
   red  int
}
func fetchData() map[string]map[string]colour {}

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

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.

type color struct {
   blue int
   red  int
}
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

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链接):

package main

import (
	"html/template"
	"os"
)

const tplStr = `<table>
    <thead>
        <tr>
            <th>Teacher</th>
            <th>Student</th>
            <th>Blue Pens</th>
            <th>Red Pens</th>
        </tr>
    </thead>
    <tbody>
	    {{range $teacher, $rows := . }}
	        {{ $first := true }}
            {{ range $student, $colors := . }}
            <tr>
                <td>{{ if $first }}{{ $first = false }}{{ $teacher }}{{ end }}</td>
                <td>{{ $student }}</td>
                <td>{{ $colors.Blue }}</td>
                <td>{{ $colors.Red }}</td>
            </tr>
            {{ end }}
        {{ end }}
    </tbody>
</table>`

type color struct {
	Blue int
	Red  int
}

func fetchData() map[string]map[string]color {
	return map[string]map[string]color{
		"joe": {
			"alex": {
				Blue: 3,
				Red:  6,
			},
			"may": {
				Blue: 2,
				Red:  6,
			},
		},
		"jena": {
			"fred": color{
				Blue: 1,
				Red:  2,
			},
		},
	}
}

func main() {
	tpl, err := template.New("table").Parse(tplStr)
	if err != nil {
		panic(err)
	}

	err = tpl.Execute(os.Stdout, fetchData())
	if err != nil {
		panic(err)
	}
}
英文:

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):

package main

import (
	&quot;html/template&quot;
	&quot;os&quot;
)

const tplStr = `&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Teacher&lt;/th&gt;
            &lt;th&gt;Student&lt;/th&gt;
            &lt;th&gt;Blue Pens&lt;/th&gt;
            &lt;th&gt;Red Pens&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
	    {{range $teacher, $rows := . }}
	        {{ $first := true }}
            {{ range $student, $colors := . }}
            &lt;tr&gt;
                &lt;td&gt;{{ if $first }}{{ $first = false }}{{ $teacher }}{{ end }}&lt;/td&gt;
                &lt;td&gt;{{ $student }}&lt;/td&gt;
                &lt;td&gt;{{ $colors.Blue }}&lt;/td&gt;
                &lt;td&gt;{{ $colors.Red }}&lt;/td&gt;
            &lt;/tr&gt;
            {{ end }}
        {{ end }}
    &lt;/tbody&gt;
&lt;/table&gt;`

type color struct {
	Blue int
	Red  int
}

func fetchData() map[string]map[string]color {
	return map[string]map[string]color{
		&quot;joe&quot;: {
			&quot;alex&quot;: {
				Blue: 3,
				Red:  6,
			},
			&quot;may&quot;: {
				Blue: 2,
				Red:  6,
			},
		},
		&quot;jena&quot;: {
			&quot;fred&quot;: color{
				Blue: 1,
				Red:  2,
			},
		},
	}
}

func main() {
	tpl, err := template.New(&quot;table&quot;).Parse(tplStr)
	if err != nil {
		panic(err)
	}

	err = tpl.Execute(os.Stdout, fetchData())
	if err != nil {
		panic(err)
	}
}

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:

确定