The ability to copy a slice to an array type of fixed size looks really convenient. Not that this isn’t impossible today with a few lines of code, but this feels natural and there are a few places I would have reached for this if it had been there. 👏
Another usage site I’ve seen is binary encoding/decoding. binary.Write requires the provided data to be fixed size values, and converting a slice to an array makes it fixed size to meet this requirement.
Even though I’m not writing much Go these days, I always appreciate these write-ups.
Yes! I came here to say the same thing. Thank you carlmjohnson, I read your posts and find them helpful!
The ability to copy a slice to an array type of fixed size looks really convenient. Not that this isn’t impossible today with a few lines of code, but this feels natural and there are a few places I would have reached for this if it had been there. 👏
For anyone curious, the current way, since go1.17, to do this conversion is
The newer form makes this conversion more straightforward to write.
and this sure as heck beat
copy
ing from the slice to the array before that:copy(array[:], slice[:len(array)])
Yeah, I think for anyone working with fixed size blocks, like hashes, this is a real QoL improvement.
Another usage site I’ve seen is binary encoding/decoding.
binary.Write
requires the provided data to be fixed size values, and converting a slice to an array makes it fixed size to meet this requirement.What’s the word on the
clear
built in function to clear a map? I know it was discussed earlier. I thought that would be an interesting addition.I believe it was approved too late in the cycle for 1.20 and will land in 1.21.