New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: go/token: API That Checks Token for Predeclared Identifiers #29694
Comments
This is already provided by
|
Works great, thanks!! I was searching the docs for "reserved", and didn't think to search |
Hmm, this doesn't seem to catch built-in type names, like Any ideas? |
For the predeclared identifiers that are not keywords, you can check whether |
It's worth noting too that it's valid to shadow Go's predeclared identifiers, though obviously you should avoid that if you need access to the predeclared objects. |
Yes -- access to the predeclared objects is required in all the cases that were throwing build-time errors! |
Perfect, thanks! Also worth documenting -- I had searched |
It sounds like no new API is needed here. |
My current Go version is:
I'm working on a kind of transpiler that reads Go packages and creates wrappers (including both new Go code and code in a dialect of Clojure) to call them.
"Canonicalizing" names into the target language means lowercasing them, so e.g.
Foo
becomesfoo
. Then, using that name within the generated Go code keeps things simple.Unfortunately,
Import
becomesimport
, which is a reserved identifier in Go. DittoDefault
,For
, etc. These end up converted to reserved identifiers, which prevents their use as function names.It's easy enough to detect and work around this (for now I'm prefixing such names with a single underscore).
But having 3rd-party code (like mine) try to keep track of reserved identifiers in Go suggests that maybe
go/ast
or some other library should offer an API that either returns the full list, an API that returns whether a given string denotes a reserved identifier, or both.The text was updated successfully, but these errors were encountered: