3 ms·
Struct tags = reflection, and reflection is nasty. They also aren't validated at compile time. Liberal use like this should be avoided.
by cedws 2y ago
Struct tags = reflection, and reflection is nasty. They also aren't validated at compile time. Liberal use like this should be avoided.
- quacker 2y agoIn what way is reflection nasty? What compile-time validation do I actually lose by using struct tags for CLI arg parsing?
- cedws 2y agoReflection is slow and bug prone. Struct tags are widely considered in the Go community to be a nasty hack yet a necessary evil. They should be used at little as possible because if you mess them up you don't get a compile error, you get a runtime error.
- 9rx 2y ago> Reflection is slow Beware of microbenchmarks, but in my tests the above package only added around 0.001 ms over the standard library flag package. Even if real world usage increases that by several orders of magnitude, it isn't going to matter. No human will ever notice.
- CamouflagedKiwi 2y agoDo you also avoid using packages like encoding/js, which make equally liberal use of struct tags and reflection?
- cedws 2y agoencoding/json does not make liberal use of struct tags, it uses only one. It's also battle tested and lives in the standard library. I don't have faith in many other projects to get bug-prone reflection code right.
- CamouflagedKiwi 2y agoIt does have a microformat within that single tag though ("x,omitempty" etc) so there is a bit more complexity than 'just one tag' indicates. Anyway, I think you're overstating how hard reflection-based code is. Yes, it is tricky and should mostly be avoided, but it's not beyond the scope of human intellect to understand (especially Go's which is pretty sensible).