<p>I have been trying to retrieve pod details &amp; fill those details into fields of the struct. However, I do get errors while filling the details into a struct.</p>
<pre class="lang-golang prettyprint-override"><code>package main

import (
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    // corev1&quot;k8s.io/api/core/v1&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/rest&quot;
)

type PodsList struct{
    podNamespace    string
    podName     string
}

func main() {
   var targetPods []TargetPodsList
   
   config, inClusterConfigError := rest.InClusterConfig()
    if inClusterConfigError != nil {
        fmt.Println(&quot;Retrieval of K8s InCluster Config not successful&quot;)
        log.Fatal(inClusterConfigError)
    }

    // Set k8s client configuration
    clientset, clientSetError := kubernetes.NewForConfig(config)
    if clientSetError != nil {
        fmt.Println(&quot;K8s Client for Incluster Configuration not successful&quot;)
        log.Fatal(clientSetError)
    }

    // Retrieve Pods for all target K8s nodes
    for index := range targetNodes.TargetNodes {
        targetPodsList, _ := clientset.CoreV1().Pods(&quot;&quot;).List(metav1.ListOptions{
            FieldSelector: &quot;spec.nodeName=&quot; + someNodeName)
        for podIndex := range targetPodsList.Items {
            targetPods.podNamespace = targetPods[podIndex].podNamespace
            targetPods.podName = targetPods[podIndex].podName
        }
    }
}
</code></pre>
<p>I know this is a basic thing which I may be missing. Please do suggest and correct my code. Thanks in advance.</p>

Sravan Kumar
<p>the code is here.</p>
<pre class="lang-golang prettyprint-override"><code>package main

import (
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;os&quot;
    &quot;path/filepath&quot;

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

func main() {
    // creates the in-cluster configuration
    config, err := rest.InClusterConfig()
    if err != nil {
        // creates the out-of-cluster config
        var kubeconfig *string
        if home := os.Getenv(&quot;HOME&quot;); 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()

        // use the current context in kubeconfig
        config, err = clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
        if err != nil {
            panic(err.Error())
        }
    }

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

    // fetch all pod name, namespace in &quot;default&quot; namespace
    pods, err := clientset.CoreV1().Pods(&quot;default&quot;).List(metav1.ListOptions{})
    for _, pod := range pods.Items {
        fmt.Printf(&quot;pod name: %s namespace: %s\n&quot;, pod.ObjectMeta.GetName(), pod.ObjectMeta.GetNamespace())
    }
}
</code></pre>
<p>run this code.</p>
<pre class="lang-bash prettyprint-override"><code>$ kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
nginx-pod    1/1     Running   0          92m
nginx-pod2   1/1     Running   0          21s

$ go run main.go
pod name: nginx-pod namespace: default
pod name: nginx-pod2 namespace: default
</code></pre>
<p>and you can see client-go examples <a href="https://github.com/kubernetes/client-go/tree/master/examples" rel="nofollow noreferrer">here</a>.</p>
