all repos — gomponents @ 1b2211f8727b1e50f542afffb1f89736f7c0b747

HTML components in pure Go

Add ExampleMap_index For getting the slice index using a counter outside the closure.

Markus Wüstenberg
commit

1b2211f8727b1e50f542afffb1f89736f7c0b747

parent

3fa5e676316d3dd68fec2e99da53ca2fe0eec9c4

1 file changed, 12 insertions(+), 0 deletions(-)

changed files
M gomponents_test.gogomponents_test.go
@@ -255,6 +255,18 @@ _ = e.Render(os.Stdout)
// Output: <ul><li>party hat</li><li>super hat</li></ul> } +func ExampleMap_index() { + items := []string{"party hat", "super hat"} + var index int + e := g.El("ul", g.Map(items, func(i string) g.Node { + e := g.El("li", g.Textf("%v: %v", index, i)) + index++ + return e + })) + _ = e.Render(os.Stdout) + // Output: <ul><li>0: party hat</li><li>1: super hat</li></ul> +} + func TestGroup(t *testing.T) { t.Run("groups multiple nodes into one", func(t *testing.T) { children := []g.Node{g.El("br", g.Attr("id", "hat")), g.El("hr")}