DEV Community

Discussion on: Array duality in Go and Rust

Collapse
 
rhymes profile image
rhymes

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.

Collapse
 
mslapek profile image
Michał Słapek • Edited

I guess that you suggest using 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 is required to calculate total output size before copying. bytes.Join applies a 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 a better solution.