为什么这里是`*invalid type`而不是`*Games`?

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

Why is this `*invalid type` instead of `*Games`?

问题

如果你在这里运行分析包:https://github.com/frederikhors/iss-goland-invalid-type,使用以下命令:

  1. go run ./analysis

它会打印出:

  1. field.Name: id - field.Type: string
  2. field.Name: name - field.Type: string
  3. field.Name: games - field.Type: *invalid type

我不明白的是为什么我得到的是 *invalid type 而不是 *Games

代码部分:

  • analyis/main.go:
  1. package main
  2. import (
  3. "go/types";
  4. "golang.org/x/tools/go/packages"
  5. )
  6. func main() {
  7. playerModel := LoadPackage("./player.go")
  8. var playerStruct *types.Struct
  9. for _, entity := range playerModel.Types.Scope().Names() {
  10. if entity == "Player" {
  11. playerStruct = GetStruct(entity, playerModel)
  12. break
  13. }
  14. }
  15. for i := 0; i < playerStruct.NumFields(); i++ {
  16. field := playerStruct.Field(i)
  17. println("field.Name: " + field.Name() + " - field.Type: " + field.Type().String())
  18. }
  19. }
  20. func LoadPackage(path string) *packages.Package {
  21. const mode = packages.NeedTypes |
  22. packages.NeedName |
  23. packages.NeedSyntax |
  24. packages.NeedFiles |
  25. packages.NeedTypesInfo |
  26. packages.NeedTypesInfo |
  27. packages.NeedModule
  28. cfg := &packages.Config{Mode: mode}
  29. pkgs, err := packages.Load(cfg, path)
  30. if err != nil {
  31. panic(err)
  32. }
  33. return pkgs[0]
  34. }
  35. func GetStruct(structName string, pkg *packages.Package) *types.Struct {
  36. foundStruct := pkg.Types.Scope().Lookup(structName)
  37. if foundStruct == nil {
  38. return nil
  39. }
  40. res, _ := foundStruct.Type().Underlying().(*types.Struct)
  41. return res
  42. }
  • player.go:
  1. type Player struct {
  2. id string
  3. name string
  4. games *Games
  5. }
  • games.go:
  1. package main
  2. type Games struct {
  3. wins []string
  4. losses []string
  5. }
英文:

If you run the analysis package here: https://github.com/frederikhors/iss-goland-invalid-type with:

  1. go run ./analysis

it will print:

  1. field.Name: id - field.Type: string
  2. field.Name: name - field.Type: string
  3. field.Name: games - field.Type: *invalid type

What I don't understand is why I'm having *invalid type instead of *Games?

Code

  • analyis/main.go:
  1. package main
  2. import (
  3. &quot;go/types&quot;
  4. &quot;golang.org/x/tools/go/packages&quot;
  5. )
  6. func main() {
  7. playerModel := LoadPackage(&quot;./player.go&quot;)
  8. var playerStruct *types.Struct
  9. for _, entity := range playerModel.Types.Scope().Names() {
  10. if entity == &quot;Player&quot; {
  11. playerStruct = GetStruct(entity, playerModel)
  12. break
  13. }
  14. }
  15. for i := 0; i &lt; playerStruct.NumFields(); i++ {
  16. field := playerStruct.Field(i)
  17. println(&quot;field.Name: &quot; + field.Name() + &quot; - field.Type: &quot; + field.Type().String())
  18. }
  19. }
  20. func LoadPackage(path string) *packages.Package {
  21. const mode = packages.NeedTypes |
  22. packages.NeedName |
  23. packages.NeedSyntax |
  24. packages.NeedFiles |
  25. packages.NeedTypesInfo |
  26. packages.NeedTypesInfo |
  27. packages.NeedModule
  28. cfg := &amp;packages.Config{Mode: mode}
  29. pkgs, err := packages.Load(cfg, path)
  30. if err != nil {
  31. panic(err)
  32. }
  33. return pkgs[0]
  34. }
  35. func GetStruct(structName string, pkg *packages.Package) *types.Struct {
  36. foundStruct := pkg.Types.Scope().Lookup(structName)
  37. if foundStruct == nil {
  38. return nil
  39. }
  40. res, _ := foundStruct.Type().Underlying().(*types.Struct)
  41. return res
  42. }
  • player.go:
  1. type Player struct {
  2. id string
  3. name string
  4. games *Games
  5. }
  • games.go:
  1. package main
  2. type Games struct {
  3. wins []string
  4. losses []string
  5. }

答案1

得分: 3

你明确地只加载了一个文件,使用LoadPackage("./player.go")。而这个文件并不是声明Games类型的文件。要加载整个包中所有类型的信息,你需要加载整个包。

你需要使用LoadPackage(".")

英文:

You are explicitly loading only a single file with LoadPackage(&quot;./player.go&quot;). And that file is not the one in which the Games type is declared. To load information on all types of a package you need to load the whole package.

You need to use LoadPackage(&quot;.&quot;).

huangapple
  • 本文由 发表于 2022年1月29日 20:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/70905567.html
匿名

发表评论

匿名网友

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

确定