HUGO
Menu
GitHub 90466 stars Mastodon

collections.NewScratch

Returns a locally scoped persistent data structure for storing and manipulating keyed values.
Syntax
collections.NewScratch
Returns
maps.Scratch
Alias
newScratch

Usage

The collections.NewScratch function returns a locally scoped persistent data structure for storing and manipulating keyed values. Use a different function or method to create a data structure with a different scope.

Methods

Use these methods on the data structure:

Set
Sets the value of the given key.
Get
(any) Returns the value of the given key.
Add
Adds the given value to the existing value of the given key. If the existing value is a slice, Add appends the given value to the slice. Otherwise, Add combines the values with Go’s + operator, such as adding two numbers or joining two strings. If the key does not exist, Add sets the value of the key.
SetInMap
Takes a key, a map key, and a value, and sets the value of the map key within the map stored under the given key.
DeleteInMap
Takes a key and a map key, and removes the map key from the map stored under the given key.
GetSortedMapValues
([]any) Returns the values of the map stored under the given key, sorted by map key.
Delete
Removes the given key.
Values
(map) Returns the backing map of the data structure. Call this method only on a data structure created with the collections.NewScratch function. Calling it on a data structure returned by the hugo.Store function or a Store method can cause a build failure due to concurrent access.

Scope

The method or function used to create the data structure determines its scope. For example, use the Store method on a Page object to create a data structure scoped to the page.

ScopeMethod or function
pagePAGE.Store
siteSITE.Store
globalhugo.Store
localcollections.NewScratch
shortcodeSHORTCODE.Store

Examples

{{ $s := collections.NewScratch }}
{{ $s.Set "greeting" "Hello" }}
{{ $s.Get "greeting" }} → Hello

Adding a string to a string:

{{ $s := collections.NewScratch }}
{{ $s.Set "greeting" "Hello" }}
{{ $s.Add "greeting" "Welcome" }}
{{ $s.Get "greeting" }} → HelloWelcome

Adding a number to a number:

{{ $s := collections.NewScratch }}
{{ $s.Set "total" 3 }}
{{ $s.Add "total" 7 }}
{{ $s.Get "total" }} → 10

Appending to a slice:

{{ $s := collections.NewScratch }}
{{ $s.Set "greetings" (slice "Hello") }}
{{ $s.Add "greetings" (slice "Welcome" "Cheers") }}
{{ $s.Get "greetings" }} → [Hello Welcome Cheers]

Setting values in a map:

{{ $s := collections.NewScratch }}
{{ $s.SetInMap "greetings" "english" "Hello" }}
{{ $s.SetInMap "greetings" "french" "Bonjour" }}
{{ $s.Get "greetings" }} → map[english:Hello french:Bonjour]

Deleting a value from a map:

{{ $s := collections.NewScratch }}
{{ $s.SetInMap "greetings" "english" "Hello" }}
{{ $s.SetInMap "greetings" "french" "Bonjour" }}
{{ $s.DeleteInMap "greetings" "english" }}
{{ $s.Get "greetings" }} → map[french:Bonjour]

Getting the map values sorted by map key:

{{ $s := collections.NewScratch }}
{{ $s.SetInMap "greetings" "english" "Hello" }}
{{ $s.SetInMap "greetings" "french" "Bonjour" }}
{{ $s.GetSortedMapValues "greetings" }} → [Hello Bonjour]

Deleting a key:

{{ $s := collections.NewScratch }}
{{ $s.Set "greeting" "Hello" }}
{{ $s.Delete "greeting" }}

Getting the backing map:

{{ $s := collections.NewScratch }}
{{ $s.SetInMap "greetings" "english" "Hello" }}
{{ $s.SetInMap "greetings" "french" "Bonjour" }}
{{ $s.Values }} → map[greetings:map[english:Hello french:Bonjour]]