Home > database >  Go template to print an array's length
Go template to print an array's length

Time:10-05

I have no Go experience. I use a tool, Podman, which is written in Go, it documents its --format option as,

-f, --format string   Format the output to a Go template or json (default "json")

I can see what I want with,

podman inspect localhost/myimage --format "{{.RootFS.Layers.length}}"

But, if I want to get the count of how many elements there are what do I do? I've tried different fields with .count and .length and .len, but I always get,

can't evaluate field FieldName in type []digest.Digest

I've also tried invoking this as {{len(RootFS.Layers)}}. I'm just kind of brute forcing it. When I do the above, I get,

ERRO[0000] Error printing inspect output: template: all inspect:1: unexpected "(" in operand

What is the right Go Template to get an array's element count?

CodePudding user response:

From the template docs you want the function len:

{{ len .RootFS.Layers }}
  • Related