为什么这个“*无效类型”而不是“*游戏”?
Why is this `*invalid type` instead of `*Games`?
如果你运行这里的分析包:https://github.com/frederikhors/iss-goland-invalid-type和:
go run ./analysis
它将打印:
field.Name: id - field.Type: string
field.Name: name - field.Type: string
field.Name: games - field.Type: *invalid type
我不明白的是为什么我有 *invalid type
而不是 *Games
?
代码
- analyis/main.go:
package main
import (
"go/types"
"golang.org/x/tools/go/packages"
)
func main() {
playerModel := LoadPackage("./player.go")
var playerStruct *types.Struct
for _, entity := range playerModel.Types.Scope().Names() {
if entity == "Player" {
playerStruct = GetStruct(entity, playerModel)
break
}
}
for i := 0; i < playerStruct.NumFields(); i++ {
field := playerStruct.Field(i)
println("field.Name: " + field.Name() + " - field.Type: " + field.Type().String())
}
}
func LoadPackage(path string) *packages.Package {
const mode = packages.NeedTypes |
packages.NeedName |
packages.NeedSyntax |
packages.NeedFiles |
packages.NeedTypesInfo |
packages.NeedTypesInfo |
packages.NeedModule
cfg := &packages.Config{Mode: mode}
pkgs, err := packages.Load(cfg, path)
if err != nil {
panic(err)
}
return pkgs[0]
}
func GetStruct(structName string, pkg *packages.Package) *types.Struct {
foundStruct := pkg.Types.Scope().Lookup(structName)
if foundStruct == nil {
return nil
}
res, _ := foundStruct.Type().Underlying().(*types.Struct)
return res
}
- player.go:
type Player struct {
id string
name string
games *Games
}
- games.go:
package main
type Games struct {
wins []string
losses []string
}
您明确地 仅 加载了带有 LoadPackage("./player.go")
的单个文件。该文件 而不是 声明了 Games
类型的文件。要加载所有类型包裹的信息,您需要加载整个包裹。
您需要使用LoadPackage(".")
。
如果你运行这里的分析包:https://github.com/frederikhors/iss-goland-invalid-type和:
go run ./analysis
它将打印:
field.Name: id - field.Type: string
field.Name: name - field.Type: string
field.Name: games - field.Type: *invalid type
我不明白的是为什么我有 *invalid type
而不是 *Games
?
代码
- analyis/main.go:
package main
import (
"go/types"
"golang.org/x/tools/go/packages"
)
func main() {
playerModel := LoadPackage("./player.go")
var playerStruct *types.Struct
for _, entity := range playerModel.Types.Scope().Names() {
if entity == "Player" {
playerStruct = GetStruct(entity, playerModel)
break
}
}
for i := 0; i < playerStruct.NumFields(); i++ {
field := playerStruct.Field(i)
println("field.Name: " + field.Name() + " - field.Type: " + field.Type().String())
}
}
func LoadPackage(path string) *packages.Package {
const mode = packages.NeedTypes |
packages.NeedName |
packages.NeedSyntax |
packages.NeedFiles |
packages.NeedTypesInfo |
packages.NeedTypesInfo |
packages.NeedModule
cfg := &packages.Config{Mode: mode}
pkgs, err := packages.Load(cfg, path)
if err != nil {
panic(err)
}
return pkgs[0]
}
func GetStruct(structName string, pkg *packages.Package) *types.Struct {
foundStruct := pkg.Types.Scope().Lookup(structName)
if foundStruct == nil {
return nil
}
res, _ := foundStruct.Type().Underlying().(*types.Struct)
return res
}
- player.go:
type Player struct {
id string
name string
games *Games
}
- games.go:
package main
type Games struct {
wins []string
losses []string
}
您明确地 仅 加载了带有 LoadPackage("./player.go")
的单个文件。该文件 而不是 声明了 Games
类型的文件。要加载所有类型包裹的信息,您需要加载整个包裹。
您需要使用LoadPackage(".")
。