<p>Hi all,</p>
<p>I'm trying to figure out how to get performance information about ESX hosts using <a href="https://github.com/vmware/govmomi">govmomi</a>. I feel like I'm super close, but I can't quite understand how to get from where I'm at to where I want to be. It's the first time I've tried to do anything mildly interesting with Go.</p>
<p>I have managed to get a response back from the API that I presume includes performance information. I created a query and invoked <a href="https://github.com/vmware/govmomi/blob/master/vim25/methods/methods.go#L8253"><code>methods.QueryPerf</code></a>, which returns a <a href="https://raw.githubusercontent.com/vmware/govmomi/master/vim25/types/types.go"><code>types.QueryPerfResponse</code></a>. <code>QueryPerfResponse</code> has a <code>Returnval</code> of type <a href="https://github.com/vmware/govmomi/blob/master/vim25/types/if.go#L2075"><code>[]types.BasePerfEntityMetricBase</code></a>.</p>
<p>When I print out the type of <code>Returnval[0]</code> using <code>%T</code>, I see that it's <code>*types.PerfEntityMetric</code>. I can see that it has exactly what I want to access (<code>SampleInfo</code> and <code>Value</code>) when I print it with <code>%+v</code>. But presumably because of the <code>[]BasePerfEntityMetricBase</code> type for <code>Returnval</code>, <code>go build</code> barks about <code>BasePerfEntityMetricBase</code> not having those properties that are of interest.</p>
<p>Anyone have any pointers for how I can convince <code>go build</code> that <code>Returnval[0]</code> is a <code>types.PerfEntityMetric</code>?</p>
<p>I've tried things like <code>types.PerfEntityMetric(res.Returnval[0])</code> and <code>res.Returnval[0].(types.PerfEntityMetric)</code>, but they don't work. I'm guessing that will be obvious to more experienced Go developers, but I can add error messages for those attempts if it will help.</p>
<p>I will work on cleaning up the code to be less derpy, and I'll post a gist when it's ready.</p>
<p>Thanks!</p>
<p>Edit: The gist, as promised: <a href="https://gist.github.com/codekoala/1a10ed7071a8e50dfcc0">https://gist.github.com/codekoala/1a10ed7071a8e50dfcc0</a></p>
<hr/>**评论:**<br/><br/>codekoala: <pre><p>I was hoping it would be as easy as something like casting, but I must be doing all the wrong things. I have very similar code working with pyvmomi, but no casting is required. I can simply dig into the various objects without fuss.</p>
<p>I'm mobile with a toddler rolling around on me, and what follows comes from memory. Please forgive mistakes. The Returnval array is an array of an interface. PerfEntityMetric does not directly implement the interface, but PerfEntityMetricBase does. PerfEntityMetric has an anonymous PerfEntityMetricBase field. I can use the GetPerfEntityMetricBase function on one of the Returnval items, but I don't know how to access the fields of interest in PerfEntityMetric from that point.</p>
<p>Very vexing.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传