有没有办法在 html 和 golang 中显示带有值结构的 map[key-string]

我有一个 map[key-string] value-struct 的数据类型,我正在尝试显示该结构的一个字段(时间)

我尝试了一个小时的各种变体,似乎无法弄清楚。将不胜感激任何指导,谢谢!

还对格式表示歉意,我是新手,请多多包涵! 我的代码


缥缈止盈
浏览 77回答 1
1回答

守着一只汪

使用 {{$value.Timing}} 而不是内部循环。// You can edit this code!// Click here and start typing.package mainimport (&nbsp; &nbsp; "os"&nbsp; &nbsp; "text/template")type A struct {&nbsp; &nbsp; Timing string}func main() {&nbsp; &nbsp; inp := `&nbsp; &nbsp; <html>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; {{ range $key,$value:= .}}&nbsp; &nbsp; &nbsp; &nbsp; Key:{{$key}}, Timing {{$value.Timing}}&nbsp; &nbsp; {{end}}&nbsp; &nbsp; </html>`&nbsp; &nbsp; valueMap := map[string]A{&nbsp; &nbsp; &nbsp; &nbsp; "key": A{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timing: "1",&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; }&nbsp; &nbsp; t, err := template.New("test").Parse(inp)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; err = t.Execute(os.Stdout, valueMap)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go