# \[solved\] Unsure how to set variable value after declaring

**URL:** <https://discourse.gohugo.io/t/solved-unsure-how-to-set-variable-value-after-declaring/1443>\
**Category:** support\
**Created:** [July 15, 2015, 7:35am UTC](https://discourse.gohugo.io/t/solved-unsure-how-to-set-variable-value-after-declaring/1443 "2015-07-15T07:35:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Weston\_McNamee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/weston_mcnamee/32/540_2.png) [@Weston\_McNamee](https://discourse.gohugo.io/u/Weston_McNamee)\
**Post date:** [July 15, 2015, 7:35am UTC](https://discourse.gohugo.io/t/solved-unsure-how-to-set-variable-value-after-declaring/1443/1 "2015-07-15T07:35:22Z")

</div>

here’s the code I’m trying to get to work:

```
{{ $maxCategories := 0 }}
{{ range $name, $taxonony := .Site.Taxonomies.categories }}
   {{ $pageCt := len $taxonomy.Pages }}
   {{ if gt $pageCt $maxCategories }}
      {{ $maxCategories := $pageCt }}
   {{ end }}
{{ end }}

```

this seems to “redeclare” a new maxCategories variable inside the range loop instead of setting the variable outside the loop. How do I make this work? Also, is there a better way to do “max” ?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 15, 2015, 8:42am UTC](https://discourse.gohugo.io/t/solved-unsure-how-to-set-variable-value-after-declaring/1443/2 "2015-07-15T08:42:43Z")

</div>

This is a known limitation (and a major gotcha) in Go templates.

To work around it, have a look at:

> **[.Scratch](http://gohugo.io/functions/scratch/)**
>
> Acts as a "scratchpad" to allow for writable page- or shortcode-scoped variables.

---

<div class="post-metadata">

**Author:** ![Weston\_McNamee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/weston_mcnamee/32/540_2.png) [@Weston\_McNamee](https://discourse.gohugo.io/u/Weston_McNamee)\
**Post date:** [July 15, 2015, 7:39pm UTC](https://discourse.gohugo.io/t/solved-unsure-how-to-set-variable-value-after-declaring/1443/3 "2015-07-15T19:39:35Z")

</div>

Thank you. I spent an hour pouring over GoLang documentation, and trying to find hints of this in Hugo documentation, finally I gave up. This definitely needs to be in the documentation.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 15, 2015, 8:19pm UTC](https://discourse.gohugo.io/t/solved-unsure-how-to-set-variable-value-after-declaring/1443/4 "2015-07-15T20:19:32Z")

</div>

Documentation could always be improved, but this has been a hot topic on this forum, so I would be really surprised if it doesn’t show up in obvious searches.
