0

背景:我使用 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 结构信息

4

1 回答 1

0

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

它不是。

我相信 Ds.Info 是 VmfsDatastoreInfo 类型

不。 if dsis a Datastorethan ds.Infois 类型BaseDatastoreInfo是接口,因此只有一个方法GetDatastoreInfo()。这就是你看到错误的原因

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

现在阅读 package reflect 的整个包文档和 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()

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

于 2018-10-11T08:31:41.533 回答