第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Go 中將地圖轉(zhuǎn)換為 html 表格

如何在 Go 中將地圖轉(zhuǎn)換為 html 表格

Go
catspeake 2022-10-24 16:23:31
我是圍棋新手并正在練習(xí),請您幫忙將地圖轉(zhuǎn)換為圍棋中的html表格嗎?我有一個(gè)函數(shù)可以從一些休息端點(diǎn)獲取一些數(shù)據(jù),并根據(jù)我從端點(diǎn)返回的結(jié)果返回一個(gè)大小可以變化的地圖。type color struct {   blue int   red  int}func fetchData() map[string]map[string]colour {}打印出這個(gè)函數(shù)的輸出看起來像這樣,但每次都會(huì)隨著更多或更少的列而變化map[joe:map[alex: {3 6} may:{2 6}] jena:map[fred: {1 2}]]我想要一個(gè)像這樣的 html 表:老師學(xué)生藍(lán)筆紅筆喬亞歷克斯36可能26耶拿弗雷德12
查看完整描述

1 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

我認(rèn)為最簡單的方法是使用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)

    }

}


查看完整回答
反對 回復(fù) 2022-10-24
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)