微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/go/187408.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐