使用名称实例化一个包变量。

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

Instantiate a package variable using just the name

问题

我正在尝试为一个包中的一些导出类型添加一些简单的验证代码,该包中充满了常量。我想遍历这个包,找出导出的常量(它们只是字符串),并验证它们的格式。

可以将其想象成我有一个名为flags的包,导出了常量FlagDoAThing = "do-a-thing"。我想遍历该包并查看这些值是什么。


举个例子,我有两个文件:

  1. // main.go
  2. func main() {
  3. cfg := &packages.Config{
  4. Mode: packages.NeedTypes,
  5. }
  6. pkgs, _ := packages.Load(cfg, "package-inspector/other")
  7. pkg := pkgs[0]
  8. scope := pkg.Types.Scope()
  9. for _, name := range scope.Names() {
  10. fmt.Println(name)
  11. obj := scope.Lookup(name)
  12. fmt.Println(obj)
  13. fmt.Println("\t", obj.Id(), obj.Type())
  14. }
  15. }

然后在我的其他包中:

  1. package other
  2. const (
  3. ExportedOne = "exported_one"
  4. ExportedTwo = "exported_two"
  5. privateOne = "private_one"
  6. )

在本地运行时(无法在 playground 中运行),我看到的输出是:

  1. go run main.go
  2. ExportedOne
  3. const package-inspector/other.ExportedOne untyped string
  4. ExportedOne untyped string
  5. ExportedTwo
  6. const package-inspector/other.ExportedTwo untyped string
  7. ExportedTwo untyped string

我想要做的是获取ExportedOne

英文:

I am trying to add some simple validation code for some exported types in a package full of constants. I want to walk the package, figure out the exported consts (which are just strings) and verify their format.

Think of it like I have a flags package that exports consts FlagDoAThing = "do-a-thing". I want to walk that package and check out what those values are.


As an example I've got two files:

  1. // main.go
  2. func main() {
  3. cfg := &packages.Config{
  4. Mode: packages.NeedTypes,
  5. }
  6. pkgs, _ := packages.Load(cfg, "package-inspector/other")
  7. pkg := pkgs[0]
  8. scope := pkg.Types.Scope()
  9. for _, name := range scope.Names() {
  10. fmt.Println(name)
  11. obj := scope.Lookup(name)
  12. fmt.Println(obj)
  13. fmt.Println("\t", obj.Id(), obj.Type())
  14. }
  15. }

Then in my other package I have

  1. package other
  2. const (
  3. ExportedOne = "exported_one"
  4. ExportedTwo = "exported_two"
  5. privateOne = "private_one"
  6. )

When running locally (couldn't get the playground to like me) I am seeing

  1. go run main.go
  2. ExportedOne
  3. const package-inspector/other.ExportedOne untyped string
  4. ExportedOne untyped string
  5. ExportedTwo
  6. const package-inspector/other.ExportedTwo untyped string
  7. ExportedTwo untyped string

What I'm trying to do is get a handle on the value of ExportedOne.

答案1

得分: 1

你可以对对象进行类型断言,将其断言为*types.Const,然后通过其Val方法访问该值。

  1. o := s.Lookup(name)
  2. if c, ok := o.(*types.Const); ok {
  3. fmt.Println(c.Val())
  4. }

示例见:https://go.dev/play/p/oXX7Mo897bk

英文:

You can type-assert the object to *types.Const and then access the value through its Val method.

  1. o := s.Lookup(name)
  2. if c, ok := o.(*types.Const); ok {
  3. fmt.Println(c.Val())
  4. }

See example: https://go.dev/play/p/oXX7Mo897bk

huangapple
  • 本文由 发表于 2022年8月5日 08:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/73243484.html
匿名

发表评论

匿名网友

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

确定