Live data from Hacker News

Small introduction to tags in Go

machiel.me

11–17 of 17 posts

Re: Small introduction to tags in Go

#11
post #2

This should be handy for frameworks to do their own logic, but I would get very annoyed if I had to read code that was cluttered with tags.

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.

Re: Small introduction to tags in Go

#12
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

#13

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.

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

#14
post #8
post #6

tags are just strings. there is no spec tag whatsoever or syntax that can be validated at compile time. A better solution would have been a proper annotation system that is part of the spec.

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"` // 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,…

> 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

#15

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.

Java annotations can be processed during compilation.

Re: Small introduction to tags in Go

#17

// VerifyPassword checks if password is valid and upgrades it if its encrypting scheme was outdated // Returns isValid, wasUpdated, error func (a *Account) VerifyPassword(password string) (bool, bool, error) { this is why we need sum types people

or just named return values

  func (a *Account) VerifyPassword(password string) (isValid, wasUpdated bool, err error) {
Post reply on HN