DRYな備忘録

Don't Repeat Yourself.

go templateで、スライスの特定インデックスの要素の、そのまたプロパティにアクセスしたい

前回

otiai10.hatenablog.com

{{index .myslice 1}}

で出力できることを知ったけど、出力するだけならまだしも、それがstructでさらにプロパティがある場合どうすんねん的なことを思った。

解決

こうじゃ

{{with $user := index . 0}}
-- {{$user.Name}} --
{{index $user.Icons 0}}
{{end}}

The Go Playground

DRYな備忘録として