Closed theduke closed 6 years ago
I just found this proposal: https://github.com/golang/go/issues/23637
Which is very similar to this one.
@theduke Is this proposal sufficiently different from #23637, or should it be closed as a dup? You can always comment on #23637.
Yeah I think this can be closed.
EDIT: this is very similar to https://github.com/golang/go/issues/23637, so maybe it should be closed.
There is recent discussion around Go2 and some larger language changes.
With this, I thought about how one of the brittle and cumbersome features of Go might be improved upon, namely: struct tags.
A note beforehand: this is not a fleshed out proposal and I'm not involved with Go development and have no knowledge about constraints or limitations for a possible implementation. So this is from the perspective of the end-user.
I'm sure I've missed a lot of potential problems, but I'm these will turn up in discussion.
Problem statement
What are the problems with struct tags:
Tags are none the less a very useful feature and important for the ecosystem.
So the question is, how can the situation be improved?
Design Overview
I propose a new tagging system that allows typed tagging of both structs and struct fields.
It is designed to:
A simple example of how the system would work:
So, it works like this:
@pkg.Fn(...)
notationImplementation
I think this could be implemented in a backwards compatible way and with a relatively low amount of work.
The
@
symbol is currently forbidden as a hard compile time error, so it can be used for this purposeTags are just regular functions and function invocations, so apart from the lexer/parser/AST changes this should be relatively straight-forward to implement. The is no complicated logic necessary.
Tag functions must be executed on package initialization and the values stored in the static type information available to the reflection system
The reflect API must be extended to allow access to the tag information.
reflect API
A rough draft of the changes to the reflect API:
Summary
This proposal suggests a new, backwards compatible tagging system for both structs and and struct fields, allowing for type safe annotation.
This would make the tag system considerably more convenient and safe to use, while not requiring a large amount of language changes.