我是圍棋新手并正在練習,請您幫忙將地圖轉換為圍棋中的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 表:
| 老師 | 學生 | 藍筆 | 紅筆 |
|---|---|---|---|
| 喬 | 亞歷克斯 | 3 | 6 |
| 可能 | 2 | 6 | |
| 耶拿 | 弗雷德 | 1 | 2 |
uj5u.com熱心網友回復:
我認為最簡單的方法是使用html/template包。text/template的檔案解釋了語法,以防您不熟悉模板引擎。
像這樣(游樂場鏈接):
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)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411151.html
標籤:
下一篇:使用GSAP的滑入和滑出效果
