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

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

從json添加到數(shù)組并在模板中執(zhí)行數(shù)據(jù)

從json添加到數(shù)組并在模板中執(zhí)行數(shù)據(jù)

Go
HUX布斯 2021-11-01 17:00:18
我有一點(diǎn)疑問!如何從 json 添加到數(shù)組數(shù)據(jù)并執(zhí)行模板。簡單的。但不工作!package mainimport (    "fmt"    "html/template"    "os"    "encoding/json")type Person struct {    Name   string    Jobs   []*Job}type Job struct {    Employer string    Role     string}const templ = `The name is {{.Name}}.{{with .Jobs}}    {{range .}}        An employer is {{.Employer}}        and the role is {{.Role}}    {{end}}{{end}}`func main() {    job1 := Job{Employer: "Monash", Role: "Honorary"}    job2 := Job{Employer: "Box Hill", Role: "Head of HE"}    byt := []byte(`{"num":6.13,"Jobs":[{"Employer": "test1", "Role": "test1"},{"Employer": "test2", "Role": "test2"}]}`)    var dat map[string]interface{}    if err := json.Unmarshal(byt, &dat); err != nil {             panic(err)        }        fmt.Println(dat)    // HOW ADD FROM ARRAY 'dat' TO STRUCT 'Job'    // LINE 54    person := Person{        Name:   "jan",        Jobs:   []*Job{&job1, &job2},    }    t := template.New("Person template")    t, err := t.Parse(templ)    checkError(err)    err = t.Execute(os.Stdout, person)    checkError(err)}func checkError(err error) {    if err != nil {        fmt.Println("Fatal error ", err.Error())        os.Exit(1)    }}在這里你可以玩/測試代碼:http : //play.golang.org/p/AB8hGLrLRy觀看第 46 行。非常感謝!
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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