英文:
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 (
	"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)
	}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论