首页 > 解决方案 > 当我传递一个指针时,我失去了该接口上所有可用的方法

问题描述

当我需要跨一个应用程序(自定义 kubernetes 控制器)传递指针时,我有一项任务 - 它每 60 秒读取一次 crd 并修改该接口。问题是当我传递一个指针时,我会丢失该接口上所有可用的方法。

它与client-go/cache cache.Store有关

有没有办法在指针上调用方法?

        package main

        import (
            "k8s.io/client-go/tools/cache"
        )

        func main() {
            store := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
            writeObject(&store)
        }

        func writeObject(store *cache.Store) {
            store.
        }

谢谢

标签: pointersgo

解决方案


store已经是一个指针,所以传递 a*cache.Store是可能的,但没有用。

接受 acache.Store时,您应该能够以正常方式访问其方法,例如

func writeObject(store cache.Store) {
    err := store.Add(...)
}

推荐阅读