<p>I want to list all pods in k8s cluster using client-go in go program.
A go program that list all pods in k8s cluster using client-go?</p>

<p>Assuming you are running the program inside the cluster use <code>InClusterConfig</code> as below and call <code>clientset.CoreV1().Pods(&quot;&quot;).List(context.TODO(), metav1.ListOptions{})</code>. Since we are not passing any value for namespace it will list all pods in all namespaces.</p>
<pre><code>func main() {
    // creates the in-cluster config
    config, err := rest.InClusterConfig()
    if err != nil {
        panic(err.Error())
    }
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    for {
        // get pods in all the namespaces by omitting namespace
        // Or specify namespace to get pods in particular namespace
        pods, err := clientset.CoreV1().Pods(&quot;&quot;).List(context.TODO(), metav1.ListOptions{})
        if err != nil {
            panic(err.Error())
        }
        fmt.Printf(&quot;There are %d pods in the cluster\n&quot;, len(pods.Items))

        // Examples for error handling:
        // - Use helper functions e.g. errors.IsNotFound()
        // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
        _, err = clientset.CoreV1().Pods(&quot;default&quot;).Get(context.TODO(), &quot;example-xxxxx&quot;, metav1.GetOptions{})
        if errors.IsNotFound(err) {
            fmt.Printf(&quot;Pod example-xxxxx not found in default namespace\n&quot;)
        } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
            fmt.Printf(&quot;Error getting pod %v\n&quot;, statusError.ErrStatus.Message)
        } else if err != nil {
            panic(err.Error())
        } else {
            fmt.Printf(&quot;Found example-xxxxx pod in default namespace\n&quot;)
        }

        time.Sleep(10 * time.Second)
    }
}
</code></pre>
