<p>Sorry I'm quite a noob at Go so hope this isn't a stupid question. I know pointers in a general sense but struggling with Go semantics.</p>
<p>I can't get this to work:</p>
<pre><code>func DeleteOldCronJob(c client.Client, ctx context.Context, namespace string, name string) error {
    cronJob := batchv1beta1.CronJob{}
    key := client.ObjectKey{
        Namespace: namespace,
        Name:      name,
    }
    return DeleteOldAny(c, ctx, name, key, &amp;cronJob)
}

func DeleteOldAny(c client.Client, ctx context.Context, name string, key client.ObjectKey, resource interface{}) error {
    err := c.Get(ctx, key, resource)

    if err == nil {
        err := c.Delete(ctx, resource)
        if err != nil {
            return err
        }
    } else {
        return err
    }

    return nil
}
</code></pre>
<p>I get an error:</p>
<pre><code>interface {} does not implement &quot;k8s.io/apimachinery/pkg/runtime&quot;.Object (missing DeepCopyObject method)
</code></pre>
<p>The point is so that I can reuse DeleteOldAny on multiple different types, to make my codebase more compact (I could just copy+paste DeleteOldCronjob and change the type).
As far as I read, pointers to interfaces in Go are usually wrong. Also, <a href="https://pkg.go.dev/k8s.io/api/batch/v1beta1?utm_source=gopls#CronJob" rel="nofollow noreferrer">the k8s type I'm importing</a> is just a struct. So, I thought since it's a struct not an interface I should pass resource a a pointer like:</p>
<pre><code>        err := c.Get(ctx, key, &amp;resource)
</code></pre>
<p>But that gives me another error:</p>
<pre><code>*interface {} is pointer to interface, not interface
</code></pre>
<p>So I'm a bit stuck. Am I doomed to copy+paste the same function for each type or is it a simple syntax mistake I'm making?</p>

james hedley
<pre><code>func (client.Reader).Get(ctx context.Context, key types.NamespacedName, obj client.Object) error
Get retrieves an obj for the given object key from the Kubernetes Cluster. obj must be a struct pointer so that obj can be updated with the response returned by the Server.
</code></pre>
<p>so the idea is right to have modular approach and stop reusing the code .</p>
<p>But implementation is wrong , the obj would be the resource you are trying to fetch from cluster which should be passed in the function as a struct pointer.</p>
<pre><code>err := c.Get(ctx, key, &amp;resource)
</code></pre>
<p>here <strong>resource</strong> should be a struct as <strong>Get , delete etc.</strong> expects as pointer to the respective object to be passed .</p>
