0

This is a follow up to this question. I figured out a solution, but this would have been MUCH easier to debug if the language didn't violate my expectation. So now I'm wondering why it did.


Give this setup command

$ podman run -d -p 1234:1234 --name evanrox alpine:3 sleep 360

The following will stringify to a map[]

$ podman inspect evanrox --format '{{ .NetworkSettings.Ports }}'
map[1234/tcp:[{ 1234}]]

While this stringifies a map of {"HostIp":"","HostPort":"1234"} to the text-values of each key-pair { 1234},

$ podman inspect evanrox --format '{{index .NetworkSettings.Ports "1234/tcp" 0 }}'

Why doesn't the above stringify to map[HostIp:"",HostPort:1234] instead?

1 Answer 1

0

What's happening here is that there are two implementations of stringification

  • The default implementation on struct, which is all the values joined by spaces, {value1, value2 ...} (you can also have custom implementations!)
  • The implementation on map, which is map[key1:"value1", key2:"value2"]

The implementation on map will dump nested structs in the {key:value, key:value} form. However, if you drill down to the struct, you'll either get the default implementation of {value1, value2 ...} (or a custom implementation). That makes templating a lot harder and it violates my understanding of templating, because you're not relying on just the language to display data, but on the random implementations of stringification.

The only ways to resolve this are to inspect the result with something that doesn't use the default implementation of Struct (or a custom implementation), with either

  • json like {{json ( index .NetworkSettings.Ports "1234/tcp" 0 ) }}'
  • printf "%#v", like {{printf "%#v" ( index .NetworkSettings.Ports "1234/tcp" 0 ) }}'

Special thanks to ikke, and svip on #go-nuts on Libera chat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.