从Go程序中获取保留字列表

huangapple go评论69阅读模式
英文:

Get list of reserved words from within a Go program

问题

我正在编写我的第一个 Golang 生成器,并希望确保某些字符串不是保留关键字。我应该从规范中复制关键字来进行检查,还是有其他更好的方法?

英文:

I am writing my first golang generator and want to make sure that some strings are not reserved keywords.
Should I copy the keywords from the spec to check against or does anyone know a better way?

答案1

得分: 3

你可以使用go/token包中的IsKeyword方法。例如:

token.Lookup("hello").IsKeyword() // false
token.Lookup("func").IsKeyword()  // true

Playground

英文:

You can use the IsKeyword method from the go/token package. For instance:

token.Lookup("hello").IsKeyword() // false
token.Lookup("func").IsKeyword()  // true

Playground

huangapple
  • 本文由 发表于 2016年12月9日 22:56:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/41063147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定