How do you access a dictionary item with a key in go?

If I have a data layout like below in a file called example.yaml, how can I access value1 with key1 in Go?

key1: value1
key2: value2

In python, if I had read the above data into a dictionary (say dict1), I would be able to simply access value1 using:


However, I am struggling to find a simple solution using hugo. The working solution I have seems to be an inefficient and round about way:

{{ with .Site.Data.example }}
  {{ range $key, $value := . }}
      {{ if eq $key "key1" }}
          {{ $value }}
      {{ end }}
  {{ end }}
{{ end }}

Is it possible to access a dict value using a key in Go, without the need for the above? Thanks in advance for any help!

Any of:

{{ site.Data.sample.key1 }}
{{ index site.Data.sample "key1" }}
{{ index site.Data "sample" "key1" }}
1 Like


Just in case this is useful to anyone - I was trying to iterate over data of the below structure:

 - item1
 - item2
 - item3
 - item4

So the solution would be:

{{ range (index .Site.Data.example "key1") }}
 {{ . }}
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.