Debian Codegraph @

docs/content/en/functions/collections/NewScratch.md

raw
1 ---
2 title: collections.NewScratch
3 description: Returns a locally scoped persistent data structure for storing and manipulating keyed values.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [newScratch]
9 returnType: maps.Scratch
10 signatures: [collections.NewScratch]
11 ---
12
13 Use the `collections.NewScratch` function to create a locally scoped persistent data structure for storing and manipulating keyed values. To create a data structure with a different [scope](g), refer to the [scope](#scope) section below.
14
15 ## Methods
16
17 Use these methods on the data structure.
18
19 `Set`
20 : Sets the value of the given key.
21
22 ```go-html-template
23 {{ $s := newScratch }}
24 {{ $s.Set "greeting" "Hello" }}
25 ```
26
27 `Get`
28 : (`any`) Gets the value of the given key.
29
30 ```go-html-template
31 {{ $s := newScratch }}
32 {{ $s.Set "greeting" "Hello" }}
33 {{ $s.Get "greeting" }} → Hello
34 ```
35
36 `Add`
37 : Adds the given value to existing value(s) of the given key.
38
39 For single values, `Add` accepts values that support Go's `+` operator. If the first `Add` for a key is an array or slice, the following adds will be appended to that list.
40
41 ```go-html-template
42 {{ $s := newScratch }}
43 {{ $s.Set "greeting" "Hello" }}
44 {{ $s.Add "greeting" "Welcome" }}
45 {{ $s.Get "greeting" }} → HelloWelcome
46 ```
47
48 ```go-html-template
49 {{ $s := newScratch }}
50 {{ $s.Set "total" 3 }}
51 {{ $s.Add "total" 7 }}
52 {{ $s.Get "total" }} → 10
53 ```
54
55 ```go-html-template
56 {{ $s := newScratch }}
57 {{ $s.Set "greetings" (slice "Hello") }}
58 {{ $s.Add "greetings" (slice "Welcome" "Cheers") }}
59 {{ $s.Get "greetings" }} → [Hello Welcome Cheers]
60 ```
61
62 `SetInMap`
63 : Takes a `key`, `mapKey` and `value` and adds a map of `mapKey` and `value` to the given `key`.
64
65 ```go-html-template
66 {{ $s := newScratch }}
67 {{ $s.SetInMap "greetings" "english" "Hello" }}
68 {{ $s.SetInMap "greetings" "french" "Bonjour" }}
69 {{ $s.Get "greetings" }} → map[english:Hello french:Bonjour]
70 ```
71
72 `DeleteInMap`
73 : Takes a `key` and `mapKey` and removes the map of `mapKey` from the given `key`.
74
75 ```go-html-template
76 {{ $s := newScratch }}
77 {{ $s.SetInMap "greetings" "english" "Hello" }}
78 {{ $s.SetInMap "greetings" "french" "Bonjour" }}
79 {{ $s.DeleteInMap "greetings" "english" }}
80 {{ $s.Get "greetings" }} → map[french:Bonjour]
81 ```
82
83 `GetSortedMapValues`
84 : (`[]any`) Returns an array of values from `key` sorted by `mapKey`.
85
86 ```go-html-template
87 {{ $s := newScratch }}
88 {{ $s.SetInMap "greetings" "english" "Hello" }}
89 {{ $s.SetInMap "greetings" "french" "Bonjour" }}
90 {{ $s.GetSortedMapValues "greetings" }} → [Hello Bonjour]
91 ```
92
93 `Delete`
94 : Removes the given key.
95
96 ```go-html-template
97 {{ $s := newScratch }}
98 {{ $s.Set "greeting" "Hello" }}
99 {{ $s.Delete "greeting" }}
100 ```
101
102 `Values`
103 : (`map`) Returns the raw backing map. Do not use with `Store` methods on a `Page` object due to concurrency issues.
104
105 ```go-html-template
106 {{ $s := newScratch }}
107 {{ $s.SetInMap "greetings" "english" "Hello" }}
108 {{ $s.SetInMap "greetings" "french" "Bonjour" }}
109
110 {{ $map := $s.Values }}
111 ```
112
113 {{% include "_common/store-scope.md" %}}