DEV Community


Discussion on: Array duality in Go and Rust

rhymes profile image

Hi Michael, thanks for the explanation of the differences between the two approaches!

Have you explored copy() in Go? I wonder if that'd work for your use case.

mslapek profile image
Michał Słapek Author

I guess that you suggest to use copy() in GetHolidayPhotos.

Yes, it could be implemented like this:

  1. Get photos junePhotos, decemberPhotos.
  2. Allocate result := make([]Photo, len(junePhotos) + len(decemberPhotos))
  3. Perform copy of junePhotos and decemberPhotos to result, calculating right destination indices.

This is correct. Notice, that it required to calculate total output size before copying. bytes.Join applies similar approach.

However, in many cases it might not be so easy to calculate final output size - consider I/O with files or database. Then append() is better solution.