Hacker News new | past | comments | ask | show | jobs | submit login

There is a speed hit to reflection, though, right?

Also, you do lose the benefit of static typing, for example, the following typo isn't picked up at compile time:

    type User struct {
        Name         string            `form:"nam"`
    }
Tags are rather ugly and don't scale to many options well. What happens when your ORM, form validation library, xml library, and json libraries each need a separate tag? That's a slight exaggeration but you could see how the apparent simplicity in the form example can quickly become anything but.

Heavy use of tags (and reflection) just makes me feel like you have to work around the language too much and I question whether you may as well just use Python, hence my original question.

I've done minimal work with Go, though, so perhaps I'm misinformed.




You're example is not a static typing issue. It's a feature of the library that the struct field doesn't have to have the same name as the form field.

I think the issue is more of a DRY thing, which could maybe be fixed with something like:

    type User struct {
        FirstName string  `form:lowercase_hyphenated`
        LastName  string  `form:lowercase_hyphenated`
    }
This hypothetical syntax would instruct the library to look for the form fields "first-name" and "last-name".


There is a speed hit to reflection. As for the tag issue: writing your own marshaler isn't actually a huge problem. I have been meaning to write my own json marshaler for a while. I personally find the solution much more paletable than many other ways of marshaling in and out of json.


There's a significant speed hit to reflection, but it's going to get lost in the noise if you're making HTTP requests.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: