反映显示不同类型而不是错误

问题描述 投票:0回答:1

背景:我使用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

go reflection vmware govmomi
1个回答
0
投票

我试图理解为什么同一个对象显示为两种不同的类型?

它不是。

我认为Ds.Info属于VmfsDatastoreInfo类型

没有。如果dsDatastore而不是ds.InfoBaseDatastoreInfo类型,这是一个界面,因此只有一种方法GetDatastoreInfo()。这就是你看错的原因

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

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

你的reflect.TypeOf(ds.Info)解析了动态类型的ds.Info(它的静态类型是BaseDatastoreInfo)。有关简单示例,请参阅https://play.golang.org/p/kgDYXv4i63Treflect.TypeOf查看其论点(这是interface {});如果它不会报告静态类型,则reflect.TypeOf将始终报告interface{})。

可能你应该使用没有反射的界面:

ds.Info.GetDatastoreInfo()

并使用该信息。无需在此反映。

© www.soinside.com 2019 - 2024. All rights reserved.