<h1><code>index(obj, idx1, [idx2..])</code></h1>
<p>You can index into a <code>map</code> with <a href="https://pkg.go.dev/text/template" rel="nofollow noreferrer"><code>index</code></a>,</p>
<blockquote>
<p><code>index</code> Returns the result of indexing its first argument by the
following arguments. Thus <code>index x 1 2 3</code> is, in Go syntax,
<code>x[1][2][3]</code>. Each indexed item must be a map, slice, or array.</p>
</blockquote>
<p>So in my case, I did</p>
<pre><code>--format '{{ index .NetworkSettings.Ports &quot;1234/tcp&quot; }}'
</code></pre>

CC BY-SA 4.0
2024-05-06T02:27:11.323Z
2024-05-06T03:53:29.703Z
2024-05-06T03:53:29.703Z
Evan Carroll
<p>When I run <code>podman inspect</code> I can get to a <code>map[...]</code> with <code>--format</code>. But indexing through it seems to be impossible.</p>
<pre class="lang-shell prettyprint-override"><code>$ podman run -d -p 1234:1234 --name evanrox alpine:3 sleep 360

$ podman inspect evanrox --format '{{ .NetworkSettings.Ports }}'
map[1234/tcp:[{ 1234}]]
</code></pre>
<p>However, these don't work,</p>
<pre><code>--format '{{ .NetworkSettings.Ports.1234/tcp }}'
--format '{{ .NetworkSettings.Ports.&quot;1234/tcp&quot; }}'
--format '{{ .NetworkSettings.Ports.[1234/tcp] }}'
--format '{{ .NetworkSettings.Ports.[&quot;1234/tcp&quot;] }}'
</code></pre>
<p>I don't write Go. What's the right syntax?</p>

<go><podman>
Go format string has numbers and special characters in map index?
Evan Carroll
# Go format string has numbers and special characters in map index?

(Asked by: username_0 on 2024-05-06)

When I run podman inspect I can get to a map[...] with --format. But indexing through it seems to be impossible.
$ podman run -d -p 1234:1234 --name evanrox alpine:3 sleep 360

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

However, these don't work,
--format '{{ .NetworkSettings.Ports.1234/tcp }}'
--format '{{ .NetworkSettings.Ports."1234/tcp" }}'
--format '{{ .NetworkSettings.Ports.[1234/tcp] }}'
--format '{{ .NetworkSettings.Ports.["1234/tcp"] }}'

I don't write Go. What's the right syntax?

**Tags:** go, podman


---

## 1 answers

### Answer by username_0 on 2024-05-06. Score: 0

index(obj, idx1, [idx2..])
You can index into a map with index,

index Returns the result of indexing its first argument by the
following arguments. Thus index x 1 2 3 is, in Go syntax,
x[1][2][3]. Each indexed item must be a map, slice, or array.

So in my case, I did
--format '{{ index .NetworkSettings.Ports "1234/tcp" }}'


---

