Live data from Hacker News

Small introduction to tags in Go

machiel.me

1–10 of 17 posts

Re: Small introduction to tags in Go

#3
This is what I like about Go. This "small introduction" is basically everything there is to know about struct tags.

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

Re: Small introduction to tags in Go

#4
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.

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

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

Re: Small introduction to tags in Go

#7

At GopherCon, Kyle Erf and Sam Helman gave a great talk about struct tags, how some people are currently using them. SourceGraph did a write up that summarized it very nicely. https://sourcegraph.com/blog/live/gophercon2015/123669868275

the official GopherCon videos aren't posted yet, but you can find a recording of this talk here: https://www.youtube.com/watch?v=JVc-dTyXDAs

Re: Small introduction to tags in Go

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

Re: Small introduction to tags in Go

#9
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,…

The `go vet` tool will find these issues. Not ideal, but it will catch tag strings that compile but incorrectly formatted.

http://godoc.org/golang.org/x/tools/cmd/vet

Re: Small introduction to tags in Go

#10
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,…

[deleted]
Post reply on HN