# Odd behavior with appending to multidimensional arrays

**URL:** <https://discourse.gohugo.io/t/odd-behavior-with-appending-to-multidimensional-arrays/34815>\
**Category:** support\
**Created:** [September 22, 2021, 3:09am UTC](https://discourse.gohugo.io/t/odd-behavior-with-appending-to-multidimensional-arrays/34815 "2021-09-22T03:09:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hekera](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hekera/32/15211_2.png) [@Hekera](https://discourse.gohugo.io/u/Hekera)\
**Post date:** [September 22, 2021, 3:09am UTC](https://discourse.gohugo.io/t/odd-behavior-with-appending-to-multidimensional-arrays/34815/1 "2021-09-22T03:09:40Z")

</div>

I’m currently using append to create multidimensional arrays, but I’m running into some unexpected behavior. Normally, if I were to “directly” append $arr2 to $arr1, the elements of $arr1 and $arr2 would be used to form a new array:

```auto
{{ $arr1 := slice "a" "b" }}
{{ $arr2 := slice "c" "d" }}
{{ $arr1 | append $arr2 }} => [a b c d]

```

If I instead want to add $arr2 as an element to $arr1, I would instead do something like:  
`{{ $arr1 | append (slice $arr2) }} => [a b [c d]]`  
which generally works.

However, in some situations involving nested arrays, directly appending $arr2 to $arr1 does not join their elements together, instead adding $arr2 as an element to $arr1:

```auto
{{ $arr1 := slice (slice "a" "b") }}
{{ $arr2 := slice "c" "d" }}
{{ $arr1 | append $arr2 }} => [[a b] c d] (expected)
{{ $arr1 | append $arr2 | append $arr2 }} => [[a b] c d [c d]] (???)

```

I’m not entirely sure what causes this behavior, besides that it might have to do with using multiple different types in the same array. I am new to Hugo and Go templating, so please let me know if I’m doing anything incorrectly.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 23, 2021, 8:33pm UTC](https://discourse.gohugo.io/t/odd-behavior-with-appending-to-multidimensional-arrays/34815/2 "2021-09-23T20:33:00Z")

</div>

Hugo’s `append` implementation:  
[https://github.com/gohugoio/hugo/blob/master/tpl/collections/append.go#L22-L37](https://github.com/gohugoio/hugo/blob/master/tpl/collections/append.go#L22-L37)

> [@Hekera](#):
>
> it might have to do with using multiple different types

I am pretty sure the answer to your question is here:  
[https://golang.org/ref/spec#Passing\_arguments\_to\_...\_parameters](https://golang.org/ref/spec#Passing_arguments_to_..._parameters)

My take on this: if the “from” slice is not [assignable](https://golang.org/ref/spec#Assignability) to the “to” slice, a _new_ slice is appended. But I’ve read the spec several times and I’m still not sure I understand it.

```auto
{{ $s1 := slice (slice "a" "b") }}
{{ $s2 := slice "c" "d" }}
{{ $s1 | append $s2 | append $s2 }} => [[a b] c d [c d]]
{{ $s1 | append ($s2 | append $s2) }} => [[a b] c d c d]

```

---

<div class="post-metadata">

**Author:** ![Hekera](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hekera/32/15211_2.png) [@Hekera](https://discourse.gohugo.io/u/Hekera)\
**Post date:** [September 25, 2021, 4:36am UTC](https://discourse.gohugo.io/t/odd-behavior-with-appending-to-multidimensional-arrays/34815/3 "2021-09-25T04:36:20Z")

</div>

Thanks for your response. I do think it has to do with assignability, though I’m not familiar enough with Golang to make full sense of the code. I can fortunately work around this quirk by using a different system for my partials, but maybe it’d be a good idea to separate `append` into `append` and `concat`? That way, `append` only adds a new element to a slice whereas `concat` only combines the elements of multiple slices, leading to more predictable behavior.
