<p>I am trying to specify the windows location for the .kube/config file for the Go client application without specifying the absolute path.</p>
<pre class="lang-golang prettyprint-override"><code>kubeconfig := flag.String(&quot;kubeconfig&quot;, &quot;%USERPROFILE%/.kube/config&quot;, &quot;location to the Kube config file&quot;)
</code></pre>
<p>Output :</p>
<pre class="lang-golang prettyprint-override"><code>panic: runtime error: invalid memory address or nil pointer dereference 
</code></pre>
<p>When I use <code>echo %USERPROFILE%</code> in a cmd, the output is <code>C:\Users\&lt;username&gt;</code>, so I thought that this is because the different usage of <code>\</code> and <code>/</code> in the code and path.
I tried to specify the path using <code>\</code> instead of <code>/</code> but it gives out a syntax error.</p>
<p>Can anyone suggest me with a solution to use windows environmental variables to specify paths in a Go application?
Thanks in advance.</p>

<p>The output of <code>flag.String</code> itself does not expand environment variables but you can use <code>os.ExpandEnv</code> to do that. However <code>os.ExpandEnv</code> requires you to use the Unix notation for environment variables, i.e. <code>$USERPROFILE</code> or <code>${USERPROFILE}</code>. You can get a clean file path for our specific OS (Windows in your case) using <code>filepath.Clean</code>.</p>
<p>Example:</p>
<pre class="lang-golang prettyprint-override"><code>kubeconfig := flag.String(&quot;kubeconfig&quot;, &quot;$USERPROFILE/.kube/config&quot;, &quot;location to the Kube config file&quot;)
fmt.Println(*kubeconfig)
fmt.Println(os.ExpandEnv(*kubeconfig))
fmt.Println(filepath.Clean(os.ExpandEnv(*kubeconfig)))
</code></pre>
<p>This will output the following on Windows:</p>
<pre><code>$USERPROFILE/.kube/config
C:\Users\username/.kube/config
C:\Users\username\.kube\config
</code></pre>
