Get/Set from Dict

Surprisingly the doc page for dict did not have examples for Get & Set. How to get the value of a key from a dict?

{{- $test := dict "k" "v" -}}
{{- warnf "%#v" ($test.Get "k") -}}

I’m getting:

execute of template failed: template ... <$test.Get>: Get is not a method but has arguments
{{ $test.k }}

or

{{ index $test "k" }}

Because those methods don’t exist in this context.

Wow, Thank you @jmooring

Could the docs for dict be updated with this info?

This is covered in the introduction to templating… required reading.
https://gohugo.io/templates/introduction/#variables

This text first speaks about “scalars, slices, maps or objects”. Summer lines later, it says

Slices and arrays are zero-based

“Arrays” have not been mentioned before, and I think that introducing them here is confusing. Understanding that “arrays” are basically the same as “slices” requires reading about both in the glossary.
I think that it might be preferable to only use the term “slice” – that’s what users create, and that arrays differ from them by having a fixed size seems to be an implementation detail which might not be relevant in the context of Hugo. Or is it?

Yeah, this is a loose end (think glossary) that I’ve been struggling with for a while.

Serialized data formats (e.g., JSON, YAML, TOML) have arrays, not slices. So we should use the word “array” in this context (e.g., “The data structure above contains an array of maps.”, or, “This front matter contains an array of tags.”).

Then change the glossary for “array” to be generic without reference to Go arrays, and add something like, “Within Hugo’s templating system, arrays are represented as slices.”

Then change the glossary for “slice” to remove reference to Go arrays, and add something like, “Within Hugo’s templating system, arrays are represented as slices.”

I’ve vacillated on this, so it would be good to put it to rest.

Thoughts?

I think that array is more common in programming. And removing the references to Go references (array/slice) from the glossary is good – most Hugo users probably don’t think about Go much.

What does “represented” mean? To introduce “represent” makes me wonder if a slice is somehow different from an array. Something like this perhaps

In Hugo, arrays are often called “slice” and created by the method slice. Slice and array mean the same thing – a numbered sequence of elements, with the numbering starting at zero.

English is not my first language, so the wording might be awkward.

They do not. I’m sorry you don’t like the word “represent”… I’ll see if I can come up with better language.

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