Showing posts from January, 2020

Golang Append

While working on some code base, I found out a weird quirk of append built-in function in Golang.

There was a string slice defined and after some operations, the function was returning two things, a string and a string slice.
... s := []string{"a", "b", "c", "d"} ... return s[1], append(s[:1], s[1+1:]...) The output is supposed to be "b" and "a", "c", "d" but what I got was "c" and "a", "c", "d". At first I thought I have discovered a bug but after reading the documentation it became clear that it is intentional. Turns out it has to do with implementation of Slice described in this blog. So, append modifies the underlying array and since arguments are evaluated from right to left, append is executed first which modified the underlying array to become "a", "c", "d" and then s[1] returns the 1st element of the modified array "c"…
We had a fun day at work today. Some new people joined and we planned a challenging treasure hunt in WeWork building for them. Then families of workmates joined us for a logo designing competition. We then played dumb charades and ate Pizzas together.