I don't anticipate that this will affect the performance at all, but it will remove a lot of conditionals that appear redundant.
In Go, append(x, someElement), where x is a nil slice, will create a new slice to contain someElement, it won't explode. Similarly, accessing a non-existent element of a map will return the zero value, which will be nil for a slice like []int, it won't explode either. Iterating or checking the length of a nil slice will work fine too, it'll just be the equivalent of doing the same operations to an empty slice.
I haven't actually tested this code because I didn't feel like setting up the whole environment, but I think it should work.
in my opinion, it's also more idiomatic to construct values once the fields are all known, which you were already doing sometimes, so I added a commit to do that in one other place.
I don't anticipate that this will affect the performance at all, but it will remove a lot of conditionals that appear redundant.
In Go,
append(x, someElement)
, wherex
is anil
slice, will create a new slice to containsomeElement
, it won't explode. Similarly, accessing a non-existent element of amap
will return the zero value, which will benil
for a slice like[]int
, it won't explode either. Iterating or checking the length of anil
slice will work fine too, it'll just be the equivalent of doing the same operations to an empty slice.I haven't actually tested this code because I didn't feel like setting up the whole environment, but I think it should work.
in my opinion, it's also more idiomatic to construct values once the fields are all known, which you were already doing sometimes, so I added a commit to do that in one other place.