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
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
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.
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 :)
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.
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.
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.
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?
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
I like your teaching style, you explain things easily and extrapolatively
@@katungiyassin9947 Wow, that's very kind of you. Thank you!
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
I love that quote!
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.
Glad I could help (although I couldn't really influence it :D)
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 :)
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.
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.
Thank you for this very useful video!
Glad it was helpful! :)
Nice and simple explanation.
Thank you 🙂
Thanks for lovely explanation, what editor you are using?
Thank you! I am currently using Zed.
Great vid! Btw what font are you using?
Thank you! I am using the Monaspace font :)
thank you
Sure thing :)
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.
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.
Good 👏
Thank you 🙏
Nice , a mobile engineer just picked up go a couple of weeks ago, is it possible to get the code snippet
Sure thing :) Happy to help here: gist.github.com/FlorianWoelki/256a609f4099e1e2a438c59932c707e1
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?
finally, none of the magic
Let us be fair structs in V are simpler and better than those in Go