草庐IT

go - Reflect showing different a different type than 错误

coder 2024-07-09 原文

背景:我正在使用 govmomi 收集 vmware 的配置。我目前正在获取我需要的数据存储信息。我需要的字段之一是磁盘 Naa。这可以在 Vmfs 字段下的 VmfsDatastoreInfo 结构中找到。

问题:我在一个范围内循环,我认为 Ds.Info 属于 VmfsDatastoreInfo 类型,所以理论上我可以通过 Ds.Info.Vmfs 获得我需要的信息。当我引用这个时,我得到了错误:

ds.Info.Vmfs undefined (type types.BaseDatastoreInfo has no field or method Vmfs)

出于好奇,我使用反射进行了探索并执行了以下操作:

fmt.Println(reflect.TypeOf(ds.Info))

输出是

*types.VmfsDatastoreInfo

我想了解为什么同一个对象显示为两种不同的类型?

编辑: 进入 ds:

c, err := govmomi.NewClient(ctx, u, true)

//Check if the connection was successful
if err != nil {
    fmt.Println(err)
}

// Create view of Datastore objects
m := view.NewManager(c.Client)

d, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"Datastore"}, true)



if err != nil {
    log.Fatal(err)
}

defer d.Destroy(ctx)

//Retrieve a list of all Virtual Machines including their summary and runtime
var dss []mo.Datastore
err = d.Retrieve(ctx, []string{"Datastore"}, []string{"info", "host"}, &dss)
if err != nil {
    log.Fatal(err)
}


for _, ds := range dss {
    fmt.Println(reflect.TypeOf(ds.Info))
    s := reflect.ValueOf(ds.Info).Elem()

    typeOfT := s.Type()

    for i := 0; i < s.NumField(); i++ {

    f := s.Field(i)

    fmt.Println(i, typeOfT.Field(i).Name, f.Type(), f.Interface())

    }

}

ds 是一种数据存储类型:

type Datastore struct {
    ManagedEntity

    Info              types.BaseDatastoreInfo        `mo:"info"`
    Summary           types.DatastoreSummary         `mo:"summary"`
    Host              []types.DatastoreHostMount     `mo:"host"`
    Vm                []types.ManagedObjectReference `mo:"vm"`
    Browser           types.ManagedObjectReference   `mo:"browser"`
    Capability        types.DatastoreCapability      `mo:"capability"`
    IormConfiguration *types.StorageIORMInfo         `mo:"iormConfiguration"`
}

根据 Govmomi 包信息,我发现了以下内容

type BaseDatastoreInfo interface {
    GetDatastoreInfo() *DatastoreInfo
}

func (b *DatastoreInfo) GetDatastoreInfo() *DatastoreInfo

type DatastoreInfo struct {
    DynamicData

    Name                   string     `xml:"name"`
    Url                    string     `xml:"url"`
    FreeSpace              int64      `xml:"freeSpace"`
    MaxFileSize            int64      `xml:"maxFileSize"`
    MaxVirtualDiskCapacity int64      `xml:"maxVirtualDiskCapacity,omitempty"`
    MaxMemoryFileSize      int64      `xml:"maxMemoryFileSize,omitempty"`
    Timestamp              *time.Time `xml:"timestamp"`
    ContainerId            string     `xml:"containerId,omitempty"`
}

Govmomi Struct Info

最佳答案

Im trying to understand why the same object is showing as two different types?

不是。

I believed Ds.Info to be of the VmfsDatastoreInfo type

没有。如果 ds 是一个 Datastoreds.InfoBaseDatastoreInfo 类型,它是一个接口(interface),因此只有一种方法 GetDatastoreInfo()。这就是您看到错误的原因

ds.Info.Vmfs undefined (type types.BaseDatastoreInfo has no field or method Vmfs)

现在阅读包 reflect 的整个包文档和 reflect.TypoOf 的文档。现在阅读https://blog.golang.org/laws-of-reflection .

您的 reflect.TypeOf(ds.Info) 解析 ds.Info 的动态类型(它的静态类型是 BaseDatastoreInfo)。有关简单示例,请参阅 https://play.golang.org/p/kgDYXv4i63T . reflect.TypeOf 在其参数(即 interface {})的内部查找;如果它始终报告静态类型,则 reflect.TypeOf 将始终报告 interface{})。

也许你应该只使用没有反射的接口(interface):

ds.Info.GetDatastoreInfo()

并使用该信息。此处无需赘述。

关于go - Reflect showing different a different type than 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52752589/

有关go - Reflect showing different a different type than 错误的更多相关文章

随机推荐