1. 27
  1. 8

    Even though I’m not writing much Go these days, I always appreciate these write-ups.

    1. 4

      Yes! I came here to say the same thing. Thank you carlmjohnson, I read your posts and find them helpful!

    2. 7

      s := []string{“a”, “b”, “c”}

      a := [3]string(s)

      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. 👏

      1. 4

        For anyone curious, the current way, since go1.17, to do this conversion is

        a := *(*[3]string)(s) // a is [3]string
        

        The newer form makes this conversion more straightforward to write.

        1. 4

          and this sure as heck beat copying from the slice to the array before that:

          copy(array[:], slice[:len(array)])

        2. 3

          Yeah, I think for anyone working with fixed size blocks, like hashes, this is a real QoL improvement.

          1. 1

            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.

        3. 4

          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.

          1. 5

            I believe it was approved too late in the cycle for 1.20 and will land in 1.21.