<p>I am new to <code>golang</code> and I am trying to list the nodes in my <code>minikube</code> cluster with the <code>client-go</code>. And I encounter the following issue:</p>
<blockquote>
<p>nodeList.Items undefined (type *invalid type has no field or method Items)compilerMissingFieldOrMethod</p>
</blockquote>
<p>And here's my code snippet for this:</p>
<pre><code>package main

import (
    &quot;context&quot;
    &quot;fmt&quot;

    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
)

func main() {
    rules := clientcmd.NewDefaultClientConfigLoadingRules()
    kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &amp;clientcmd.ConfigOverrides{})
    config, err := kubeconfig.ClientConfig()
    if err != nil {
        panic(err)
    }
    clientset := kubernetes.NewForConfigOrDie(config)

    nodeList, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})

    if err != nil {
        panic(err)
    }

    for _, node := range nodeList.Items {
        fmt.Printf(&quot;%s\n&quot;, node.Name)
    }
}

</code></pre>
<p>Can someone help me what is the problem here?</p>
<p>Thank you!</p>

Jananath Banuka
<p>It looks like the clientset is not created from your kubeconfig. I would suggest you to create the clientset in the following way. I have used out of cluster config here to create the clientset. You can also create it using <a href="https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration" rel="nofollow noreferrer">InclusterConfig</a>.</p>
<pre><code>package main

import (
    &quot;context&quot;
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/util/homedir&quot;
    &quot;path/filepath&quot;

    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
)

func main() {
    // parse the .kubeconfig file
    var kubeconfig *string
    if home := homedir.HomeDir(); home != &quot;&quot; {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
    } else {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
    }
    flag.Parse()

    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
    if err != nil {
        panic(err)
    }

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err)
    }

    nodeList, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})

    if err != nil {
        panic(err)
    }

    for _, node := range nodeList.Items {
        fmt.Printf(&quot;%s\n&quot;, node.Name)
    }
}
</code></pre>
