Golang cannot take the address of

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

..\server\handlers\adapter.go:70: 
cannot call pointer method on clusterQuota.Hard[admin.ResourceRequestsCPU]
..\server\handlers\adapter.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值