I guess that you suggest using copy() in GetHolidayPhotos.
Yes, it could be implemented like this:
Get photos junePhotos, decemberPhotos.
Allocate result := make([]Photo, len(junePhotos) + len(decemberPhotos))
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.
I guess that you suggest using
copy()
inGetHolidayPhotos
.Yes, it could be implemented like this:
junePhotos
,decemberPhotos
.result := make([]Photo, len(junePhotos) + len(decemberPhotos))
junePhotos
anddecemberPhotos
toresult
, 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.