<p>Following the k8s/controller-runtime/client example code (see <a href="https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/client#example-Client-Create" rel="nofollow noreferrer">here</a>), which goes a bit like this</p>
<pre class="lang-golang prettyprint-override"><code>var c client.Client

func main() {
    // Using a typed object.
    pod := &amp;corev1.Pod{
        ObjectMeta: metav1.ObjectMeta{
            Namespace: &quot;namespace&quot;,
            Name:      &quot;name&quot;,
        },
        Spec: corev1.PodSpec{
            Containers: []corev1.Container{
                {
                    Image: &quot;nginx&quot;,
                    Name:  &quot;nginx&quot;,
                },
            },
        },
    }
    // c is a created client.
    _ = c.Create(context.Background(), pod) // nil deref here  
}
</code></pre>
<p>I get a nullptr dereference on <code>_ = c.Create(context.Background(), pod)</code>. To me this makes sense, since I declared <code>c</code>, but never initialised it. However the example code also does that. What is going on here?</p>

<p>The correct way to initialise the client can be found here: <a href="https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/client#example-New" rel="nofollow noreferrer">https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/client#example-New</a></p>
<pre class="lang-golang prettyprint-override"><code>cl, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
    fmt.Println(&quot;failed to create client&quot;)
    os.Exit(1)
}
</code></pre>
