Answers: <p>What's happening here is that there are two implementations of stringification</p>
  <ul>
  <li>The default implementation on struct, which is all the values joined by spaces, <code>{value1, value2 ...}</code> (you can also have custom implementations!)</li>
  <li>The implementation on map, which is <code>map[key1:&quot;value1&quot;, key2:&quot;value2&quot;]</code></li>
  </ul>
  <p>The implementation on map will dump nested structs in the <code>{key:value, key:value}</code> form. However, if you drill down to the struct, you'll either get the default implementation of <code>{value1, value2 ...}</code> (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.</p>
  <p>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</p>
  <ul>
  <li><code>json</code> like <code>{{json ( index .NetworkSettings.Ports &quot;1234/tcp&quot; 0 ) }}'</code></li>
  <li><code>printf &quot;%#v&quot;</code>, like <code>{{printf &quot;%#v&quot; ( index .NetworkSettings.Ports &quot;1234/tcp&quot; 0 ) }}'</code></li>
  </ul>
  <hr />
  <p>Special thanks to ikke, and svip on #go-nuts on Libera chat</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-05-06T05:38:04.353Z
Answers: 2024-05-06T05:38:04.353Z
Answers: Evan Carroll
Body: <p><em>This is a follow up to <a href="https://devops.stackexchange.com/q/19256/18965">this question</a>. I figured out a solution, but this would have been <strong>MUCH</strong> easier to debug if the language didn't violate my expectation. So now I'm wondering why it did.</em></p>
  <hr />
  <p>Give this setup command</p>
  <pre class="lang-shell prettyprint-override"><code>$ podman run -d -p 1234:1234 --name evanrox alpine:3 sleep 360
  </code></pre>
  <p>The following will stringify to a <code>map[]</code></p>
  <pre class="lang-shell prettyprint-override"><code>$ podman inspect evanrox --format '{{ .NetworkSettings.Ports }}'
  map[1234/tcp:[{ 1234}]]
  </code></pre>
  <p>While this stringifies a map of <code>{&quot;HostIp&quot;:&quot;&quot;,&quot;HostPort&quot;:&quot;1234&quot;}</code> to the text-values of each key-pair <code>{ 1234}</code>,</p>
  <pre class="lang-shell prettyprint-override"><code>$ podman inspect evanrox --format '{{index .NetworkSettings.Ports &quot;1234/tcp&quot; 0 }}'
  </code></pre>
  <p>Why doesn't the above stringify to <code>map[HostIp:&quot;&quot;,HostPort:1234]</code> instead?</p>
  
Tags: <go><podman>
Title: Why do some things stringify to their values and others strinigify to a textual representation of the data structure?
UserDisplayName: Evan Carroll
ThreadText: # Why do some things stringify to their values and others strinigify to a textual representation of the data structure?
  
  (Asked by: username_0 on 2024-05-06)
  
  *This is a follow up to [this question](https://devops.stackexchange.com/q/19256/18965). 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
  
  ```shell
  $ podman run -d -p 1234:1234 --name evanrox alpine:3 sleep 360
  
  ```
  
  The following will stringify to a `map[]`
  
  ```shell
  $ 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}`,
  
  ```shell
  $ podman inspect evanrox --format '{{index .NetworkSettings.Ports "1234/tcp" 0 }}'
  
  ```
  
  Why doesn't the above stringify to `map[HostIp:"",HostPort:1234]` instead?
  
  **Tags:** go, podman
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-05-06. Score: 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
  
  
  ---
  
  