加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang cannot take the address of

发布时间:2020-12-16 18:22:31 所属栏目:大数据 来源:网络整理
导读:今天在使用kubernetes/apimachinery下/pkg/api/resource中的Quantity接收k8s资源信息的时候,报出如下错误: ..serverhandlersadapter.go: 70 : cannot call pointer method on clusterQuota . Hard [ admin . ResourceRequestsCPU ] .. server handle

今天在使用kubernetes/apimachinery下/pkg/api/resource中的Quantity接收k8s资源信息的时候,报出如下错误:

..serverhandlersadapter.go:70: 
cannot call pointer method on clusterQuota.Hard[admin.ResourceRequestsCPU] ..serverhandlersadapter.go:70: 
cannot take the address of clusterQuota.Hard[admin.ResourceRequestsCPU]

具体出错代码如下:

test:= clusterQuota.Hard[admin.ResourceRequestsCPU].Value()
fmt.Println(test)

Quantity结构及Value()方法如下:

type Quantity struct {
    // i is the quantity in int64 scaled form,if d.Dec == nil
    i int64Amount
    // d is the quantity in inf.Dec form if d.Dec != nil
    d infDecAmount
    // s is the generated value of this quantity to avoid recalculation
    s string

    // Change Format at will. See the comment for Canonicalize for
    // more details.
    Format
}
// Value returns the value of q; any fractional part will be lost.
func (q *Quantity) Value() int64 {
    return q.ScaledValue(0)
}

既然调用方法指定的是指针,那么这么调用是否可以?

ii := (&clusterQuota.Hard[tenant_admin.ResourceRequestsCPU]).Value()
fmt.Println(ii)

编译发现还是不行,那应该怎么处理呢?
其实会发现,取出变量的地址,需要两步:

hardRequestCpu := clusterQuota.Hard[tenant_admin.ResourceRequestsCPU]
fmt.Println((&hardRequestCpu).Value())

本文参考:
https://stackoverflow.com/questions/10535743/address-of-a-temporary-in-go

个人微信公众号:

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读