Make your context keys safer with this one simple trick

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 2024
  • How to improve your context keys with virtually no effort.

ความคิดเห็น • 7

  • @cmelgarejo
    @cmelgarejo 10 หลายเดือนก่อน +2

    The awkward wait killed me 🤣

  • @kowalkem
    @kowalkem 10 หลายเดือนก่อน +1

    Would there be something preventing us from using a pointer to empty struct as key?

    • @boldlygo
      @boldlygo  10 หลายเดือนก่อน +1

      Good question. And yes, there is a reason not to use a pointer to the empty struct: They're all identical! So this would actually make the problem worse, not better.
      See it in the playground: go.dev/play/p/8OoLFRu3NnM

    • @kowalkem
      @kowalkem 10 หลายเดือนก่อน +1

      @boldlygo thank you for that info. Is this behaviour documented somewhere? Can't find it in the spec at first glance.
      And another thing - would pointers to the empty structs of different types work? - watching it in the playground, no.
      Very interesting behaviour.
      BTW loving your podcast 👌

    • @boldlygo
      @boldlygo  10 หลายเดือนก่อน +2

      The relevant sentence in the spec reads "Two distinct zero-size variables may have the same address in memory."
      IOW, it's not guaranteed. An implementation may choose to have pointers to two empty structs (or other zero-size variables) point to different memory addresses, but you cannot rely on this.
      Dave Cheney wrote more extensively about this, and related behaviors of empty structs, if you're interested: dave.cheney.net/2014/03/25/the-empty-struct

    • @kowalkem
      @kowalkem 10 หลายเดือนก่อน +2

      Many thanks for clarification@@boldlygo