Creating custom struct tags in Golang is awesome!

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ • 32

  • @FloWoelki
    @FloWoelki  3 місяці тому +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: ua-cam.com/video/W4njY-VzkUU/v-deo.html

    • @katungiyassin9947
      @katungiyassin9947 3 місяці тому +1

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

    • @FloWoelki
      @FloWoelki  3 місяці тому

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

  • @grim.reaper
    @grim.reaper 16 днів тому

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

    • @FloWoelki
      @FloWoelki  15 днів тому

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

  • @1sandesh1
    @1sandesh1 4 місяці тому +13

    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  4 місяці тому +1

      I love that quote!

  • @EnmaDarei
    @EnmaDarei 3 місяці тому +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  3 місяці тому +1

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

  • @sascha1461
    @sascha1461 Місяць тому

    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  Місяць тому

      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.

  • @nasko235679
    @nasko235679 22 дні тому

    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.

  • @gungun974
    @gungun974 4 місяці тому +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  4 місяці тому

      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.

  • @higiniofuentes2551
    @higiniofuentes2551 3 місяці тому

    Thank you for this very useful video!

    • @FloWoelki
      @FloWoelki  3 місяці тому +1

      Glad it was helpful! :)

  • @ggadoctb
    @ggadoctb Місяць тому

    Thanks for lovely explanation, what editor you are using?

    • @FloWoelki
      @FloWoelki  29 днів тому

      Thank you! I am currently using Zed.

  • @WoogleMovie
    @WoogleMovie 4 місяці тому

    Nice and simple explanation.

  • @thanapongangkha1417
    @thanapongangkha1417 3 місяці тому

    Great vid! Btw what font are you using?

    • @FloWoelki
      @FloWoelki  3 місяці тому

      Thank you! I am using the Monaspace font :)

  • @kirindev
    @kirindev 2 місяці тому

    thank you

  • @anon_y_mousse
    @anon_y_mousse 3 місяці тому

    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?

  • @selvamp5775
    @selvamp5775 4 місяці тому

    Good 👏

  • @adekoyegbolahan1069
    @adekoyegbolahan1069 4 місяці тому

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

    • @FloWoelki
      @FloWoelki  4 місяці тому +2

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

  • @apidas
    @apidas 3 місяці тому

    finally, none of the magic

  • @katungiyassin9947
    @katungiyassin9947 3 місяці тому

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