Creating custom struct tags in Golang is awesome!

Поделиться
HTML-код
  • Опубликовано: 1 фев 2025
  • НаукаНаука

Комментарии • 36

  • @FloWoelki
    @FloWoelki  6 месяцев назад +4

    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: ruclips.net/video/W4njY-VzkUU/видео.html

    • @katungiyassin9947
      @katungiyassin9947 5 месяцев назад +1

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

    • @FloWoelki
      @FloWoelki  5 месяцев назад

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

  • @EnmaDarei
    @EnmaDarei 5 месяцев назад +4

    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  5 месяцев назад +1

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

  • @grim.reaper
    @grim.reaper 2 месяца назад

    I found your channel just days back and I can’t thank you enough for these videos!!
    This is great!! 😭

    • @FloWoelki
      @FloWoelki  2 месяца назад

      Wow, thank you so much! I greatly appreciate it!

  • @1sandesh1
    @1sandesh1 6 месяцев назад +18

    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  6 месяцев назад +2

      I love that quote!

  • @abdushakoor0099
    @abdushakoor0099 Месяц назад

    video editing in addition to the content quality is awesome

  • @meka4996
    @meka4996 Месяц назад

    Amazing! Thanks

  • @sascha1461
    @sascha1461 3 месяца назад

    great video. I've been using golang for a couple of months now and that's one of the things I was still wondering how it works. I don't think I have a use case yet in my projects for it but I'll keep it in mind :)

    • @FloWoelki
      @FloWoelki  3 месяца назад

      Good stuff! It's pretty hard to come up with a use case. But it would be a good use case for more straightforward library embedding.

  • @higiniofuentes2551
    @higiniofuentes2551 5 месяцев назад

    Thank you for this very useful video!

    • @FloWoelki
      @FloWoelki  5 месяцев назад +1

      Glad it was helpful! :)

  • @WoogleMovie
    @WoogleMovie 6 месяцев назад

    Nice and simple explanation.

    • @FloWoelki
      @FloWoelki  6 месяцев назад

      Thank you 🙂

  • @nasko235679
    @nasko235679 2 месяца назад

    That's a great example. I wonder if in real code bases people manually write out the validation like you did or if they're using a library like go validator. Coming from JS that looks like a lot of code for simple validation lol.

  • @ggadoctb
    @ggadoctb 3 месяца назад

    Thanks for lovely explanation, what editor you are using?

    • @FloWoelki
      @FloWoelki  3 месяца назад

      Thank you! I am currently using Zed.

  • @gungun974
    @gungun974 6 месяцев назад +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  6 месяцев назад

      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 5 месяцев назад

    Great vid! Btw what font are you using?

    • @FloWoelki
      @FloWoelki  5 месяцев назад

      Thank you! I am using the Monaspace font :)

  • @kirindev
    @kirindev 5 месяцев назад

    thank you

  • @adekoyegbolahan1069
    @adekoyegbolahan1069 6 месяцев назад

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

    • @FloWoelki
      @FloWoelki  6 месяцев назад +2

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

  • @selvamp5775
    @selvamp5775 6 месяцев назад

    Good 👏

    • @FloWoelki
      @FloWoelki  6 месяцев назад

      Thank you 🙏

  • @anon_y_mousse
    @anon_y_mousse 6 месяцев назад

    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?

  • @apidas
    @apidas 5 месяцев назад

    finally, none of the magic

  • @mr.prince8701
    @mr.prince8701 Месяц назад

    I might alone but this seems like overkill for something that should be so simple. I get that these tags are useful for JSON and db stuff, but this is just way too verbose.

  • @katungiyassin9947
    @katungiyassin9947 5 месяцев назад

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