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,
Addappends the given value to the slice. Otherwise,Addcombines the values with Go’s+operator, such as adding two numbers or joining two strings. If the key does not exist,Addsets 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 thecollections.NewScratchfunction. Calling it on a data structure returned by thehugo.Storefunction or aStoremethod 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.
| Scope | Method or function |
|---|---|
| page | PAGE.Store |
| site | SITE.Store |
| global | hugo.Store |
| local | collections.NewScratch |
| shortcode | SHORTCODE.Store |
Examples
{{ $s := collections.NewScratch }}
{{ $s.Set "greeting" "Hello" }}
{{ $s.Get "greeting" }} → HelloAdding a string to a string:
{{ $s := collections.NewScratch }}
{{ $s.Set "greeting" "Hello" }}
{{ $s.Add "greeting" "Welcome" }}
{{ $s.Get "greeting" }} → HelloWelcomeAdding a number to a number:
{{ $s := collections.NewScratch }}
{{ $s.Set "total" 3 }}
{{ $s.Add "total" 7 }}
{{ $s.Get "total" }} → 10Appending 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]]Last updated:
November 8, 2026
:
content: Apply style guide to Stores and NewScratch (57b69f294)
Improve this page