<p>I want to access my Kubernetes cluster API in Go to run <code>kubectl</code> command to get available namespaces in my k8s cluster which is running on google cloud.</p>

<p>My sole purpose is to get namespaces available in my cluster by running <code>kubectl</code> command: kindly let me know if there is any alternative.</p>

MSA
<p>You can start with <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer"><code>kubernetes/client-go</code></a>, the Go client for Kubernetes, made for talking to a kubernetes cluster. (not through kubectl though: directly through the Kubernetes API)</p>

<p>It includes a <a href="https://github.com/kubernetes/client-go/blob/03bfb9bdcfe5482795b999f39ca3ed9ad42ce5bb/listers/core/v1/namespace.go#L28-L35" rel="nofollow noreferrer"><code>NamespaceLister</code>, which helps list Namespaces</a>.</p>

<p>See "<a href="https://medium.com/programming-kubernetes/building-stuff-with-the-kubernetes-api-part-4-using-go-b1d0e3c1c899" rel="nofollow noreferrer">Building stuff with the Kubernetes API — Using Go</a>" from <strong><a href="https://twitter.com/VladimirVivien" rel="nofollow noreferrer">Vladimir Vivien</a></strong></p>

<p><a href="https://i.stack.imgur.com/xVxMG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xVxMG.png" alt="https://cdn-images-1.medium.com/max/1000/1*4noxYkVSvMPmlbt1kSOIHw.png"></a></p>

<p><a href="https://stackoverflow.com/users/396567/michael-hausenblas">Michael Hausenblas</a> (Developer Advocate at Red Hat) proposes <a href="https://stackoverflow.com/questions/52325091/how-to-access-the-kubernetes-api-in-go-and-run-kubectl-commands#comment91597517_52325186">in the comments</a> documentations with <a href="http://using-client-go.cloudnative.sh/" rel="nofollow noreferrer"><code>using-client-go.cloudnative.sh</code></a></p>

<blockquote>
  <p>A versioned collection of snippets showing how to use <code>client-go</code>. </p>
</blockquote>
