Creating custom struct tags in Golang is awesome!

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

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

  • @FloWoelki
    @FloWoelki  2 หลายเดือนก่อน +3

    I've recently uploaded a new video where we add unit tests to this validate custom struct functionality. Feel free to check it out here: th-cam.com/video/W4njY-VzkUU/w-d-xo.html

    • @katungiyassin9947
      @katungiyassin9947 2 หลายเดือนก่อน +1

      I like your teaching style, you explain things easily and extrapolatively

    • @FloWoelki
      @FloWoelki  2 หลายเดือนก่อน

      @@katungiyassin9947 Wow, that's very kind of you. Thank you!

  • @1sandesh1
    @1sandesh1 2 หลายเดือนก่อน +12

    In some scenarios, struct tags offer readability and that lends itself well to its usage in data marshaling/unmarshaling. However, for any other use, a developer should always consider the runtime costs associated with reflection. Most of the time writing specific logic (say for validation) is often clearer.
    "Clear is better than clever"
    "Reflection is never clear"
    - Go proverbs

    • @FloWoelki
      @FloWoelki  2 หลายเดือนก่อน +1

      I love that quote!

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

    Really funny how youtube recommended this video to me right when I was working on a go project that became a lot easier with custom struct tags. thanks, algorithm.

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

      Glad I could help (although I couldn't really influence it :D)

  • @higiniofuentes2551
    @higiniofuentes2551 2 หลายเดือนก่อน

    Thank you for this very useful video!

    • @FloWoelki
      @FloWoelki  2 หลายเดือนก่อน +1

      Glad it was helpful! :)

  • @gungun974
    @gungun974 2 หลายเดือนก่อน +1

    Okay thanks now I know tags are not that hard to used.
    But personally I don’t know yet if I want to uses them. For json serialization they are incredibly. For validation I still prefer to manual create a function and use some if else to handle my requirements (I create my own library btw for that task).
    But apart for that I can’t imagine a problem that can be solved with tags.

    • @FloWoelki
      @FloWoelki  2 หลายเดือนก่อน

      That's awesome to hear!
      Struct tags are basically useful whenever you want to attach metadata to some struct fields.
      And there are some use cases like any sort of serialization/deserialization, database operations, validation, and configuration management, and they are really useful for library or framework developers (e.g., gorm, protobuf, csv).
      I think it is still interesting to know how these struct tags even work.

  • @thanapongangkha1417
    @thanapongangkha1417 หลายเดือนก่อน

    Great vid! Btw what font are you using?

    • @FloWoelki
      @FloWoelki  หลายเดือนก่อน

      Thank you! I am using the Monaspace font :)

  • @WoogleMovie
    @WoogleMovie 2 หลายเดือนก่อน

    Nice and simple explanation.

    • @FloWoelki
      @FloWoelki  2 หลายเดือนก่อน

      Thank you 🙂

  • @anon_y_mousse
    @anon_y_mousse 2 หลายเดือนก่อน

    This seems like a really weird concept to me. You're using reflection to check for strings and then checking values based on knowing way too much about the struct anyway. For instance, you would have to know to check for min and max requirements of the struct and then check the values. I just have a few questions here.
    How do you generate assembly from Go code? I looked on the main website and either the instructions are out of date or my copy of Go is, because it didn't work at all. I've got version 1.16.5 if that helps. And does this validate at compile time or run time? If it validates at runtime, then I would think it'd be easier to just write a validator for each struct anyway. Does Go have something akin to C++'s constexpr and consteval which would enable you to force it at compile time, or does turning on optimizations do that anyway?
    I guess the only question I have left at this point is, is there another purpose for these custom tags other than validation and if so could you make a video demonstrating that usage?

  • @adekoyegbolahan1069
    @adekoyegbolahan1069 2 หลายเดือนก่อน

    Nice , a mobile engineer just picked up go a couple of weeks ago, is it possible to get the code snippet

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

      Sure thing :) Happy to help here: gist.github.com/FlorianWoelki/256a609f4099e1e2a438c59932c707e1

  • @kirindev
    @kirindev หลายเดือนก่อน

    thank you

    • @FloWoelki
      @FloWoelki  หลายเดือนก่อน

      Sure thing :)

  • @selvamp5775
    @selvamp5775 2 หลายเดือนก่อน

    Good 👏

    • @FloWoelki
      @FloWoelki  2 หลายเดือนก่อน

      Thank you 🙏

  • @apidas
    @apidas หลายเดือนก่อน

    finally, none of the magic

  • @katungiyassin9947
    @katungiyassin9947 2 หลายเดือนก่อน

    Let us be fair structs in V are simpler and better than those in Go