Re:Small introduction to tags in Go
by jamesfisher on Tuesday July 21, 2015 @08:15PM
(#9926846)
Why is this "tag" system considered a good feature? It seems so arbitrary. It's used exclusively for reflective logic which should instead be done at compile-time via a macro system or a system like Template Haskell, which would generate instances like `ToJson`/`FromJson` for a given struct. As the article mentions, Go tags are similar to Java annotations: a mis-feature motivated by the absence of a macro system.
Re:Small introduction to tags in Go
by skybrian on Tuesday July 21, 2015 @08:33PM
(#9926931)
You're right that reflection and compile time code generation are somewhat "dual" in the sense that you can often implement a feature using one or the other.
But that doesn't mean everyone should make the same choice. Go doesn't have macros or templates. It would make the language much larger and many Go development tools would have to be modified to deal with the complexity. This is a minimal language tweak that was far easier to implement, and devtools that don't care about tags can ignore them.
Re:Small introduction to tags in Go
by toufka on Tuesday July 21, 2015 @03:33PM
(#9925085)
This is the one problem.
Spent a whole day trying to figure out why only the first tag in the struct was 'active':
Name string `xml:"name", db:"name"` //<--- XML works, DB doesn't
Name string `db:"name", xml:"name"` //<--- DB works, XML doesn't
The answer was, going from memory, I added a comma (,) between the tags. Should have been:
Name string `xml:"name" db:"name"`
Stupid simple mistake, but really hard to track down. There was no compiler error, the second tag failed silently, and the first tag worked, so it was really hard to find the bad code.
That along with my favorite: quote-wars. Many-a-time did I forget the quotes on the tag itself only to have it fail (without warning again...):
Name string `xml:name db:"name"` //<--- DB works, XML doesn't
Re:Small introduction to tags in Go
by AdieuToLogic on Tuesday July 21, 2015 @11:03PM
(#9927516)
> Stupid simple mistake, but really hard to track down. There was no compiler error, the second tag failed silently, and the first tag worked, so it was really hard to find the bad code.
IMHO, this is why encoding meta-information into strings fail in every incarnation I've experienced. For a retro example, we need look no further than the various data base libraries which place the SQL in strings.
Bad things happen when the compiler is crippled by "tunneling" statements through strings.
Re:Small introduction to tags in Go
by Zikes on Tuesday July 21, 2015 @01:28PM
(#9924014)
Simplicity and minimalism are core values in Go, both in the language itself and the community and developers that use it, so clutter tends not to be a concern.
That aside, tags are currently in use in several ORM frameworks with a fair amount of success.
Re:Small introduction to tags in Go
by Sphax on Tuesday July 21, 2015 @05:49PM
(#9926088)
From experience you don't tend to see tags very often other than when decoding/encoding JSON. If you use an ORM maybe you use tags as well.
Other than that, their usage (at least to me after working with Go for a year now) is limited to things like Toml/YAML decoding for configuration, or (shameless plug) configuration with https://github.com/vrischmann/envconfig.
Bonus, Gorethink tags [0] and how we use them at Lavaboom [1].
0: https://github.com/dancannon/gorethink#encodingdecoding-stru...
1: https://github.com/lavab/api/tree/master/models
SourceGraph did a write up that summarized it very nicely. https://sourcegraph.com/blog/live/gophercon2015/123669868275
But that doesn't mean everyone should make the same choice. Go doesn't have macros or templates. It would make the language much larger and many Go development tools would have to be modified to deal with the complexity. This is a minimal language tweak that was far easier to implement, and devtools that don't care about tags can ignore them.
A better solution would have been a proper annotation system that is part of the spec.
Spent a whole day trying to figure out why only the first tag in the struct was 'active':
The answer was, going from memory, I added a comma (,) between the tags. Should have been: Stupid simple mistake, but really hard to track down. There was no compiler error, the second tag failed silently, and the first tag worked, so it was really hard to find the bad code.That along with my favorite: quote-wars. Many-a-time did I forget the quotes on the tag itself only to have it fail (without warning again...):
http://godoc.org/golang.org/x/tools/cmd/vet
IMHO, this is why encoding meta-information into strings fail in every incarnation I've experienced. For a retro example, we need look no further than the various data base libraries which place the SQL in strings.
Bad things happen when the compiler is crippled by "tunneling" statements through strings.
That aside, tags are currently in use in several ORM frameworks with a fair amount of success.
Other than that, their usage (at least to me after working with Go for a year now) is limited to things like Toml/YAML decoding for configuration, or (shameless plug) configuration with https://github.com/vrischmann/envconfig.
func (a *Account) VerifyPassword(password string) (bool, bool, error) {
this is why we need sum types people