如何在 Go 中将地图转换为 html 表格

我是围棋新手并正在练习,请您帮忙将地图转换为围棋中的html表格吗?我有一个函数可以从一些休息端点获取一些数据,并根据我从端点返回的结果返回一个大小可以变化的地图。


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

老师学生蓝笔红笔
亚历克斯36

可能26
耶拿弗雷德12


catspeake
浏览 86回答 1
1回答

慕运维8079593

我认为最简单的方法是使用html/template包。text/template的文档解释了语法,以防您不熟悉模板引擎。像这样(游乐场链接):package mainimport (&nbsp; &nbsp; "html/template"&nbsp; &nbsp; "os")const tplStr = `<table>&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Teacher</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Student</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Blue Pens</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Red Pens</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; {{range $teacher, $rows := . }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $first := true }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ range $student, $colors := . }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{ if $first }}{{ $first = false }}{{ $teacher }}{{ end }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $student }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $colors.Blue }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $colors.Red }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ end }}&nbsp; &nbsp; &nbsp; &nbsp; {{ end }}&nbsp; &nbsp; </tbody></table>`type color struct {&nbsp; &nbsp; Blue int&nbsp; &nbsp; Red&nbsp; int}func fetchData() map[string]map[string]color {&nbsp; &nbsp; return map[string]map[string]color{&nbsp; &nbsp; &nbsp; &nbsp; "joe": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "alex": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Blue: 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Red:&nbsp; 6,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "may": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Blue: 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Red:&nbsp; 6,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "jena": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "fred": color{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Blue: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Red:&nbsp; 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; tpl, err := template.New("table").Parse(tplStr)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; err = tpl.Execute(os.Stdout, fetchData())&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go