如何在PowerShell中修改GOPATH

huangapple go评论102阅读模式
英文:

how to modify GOPATH in powershell

问题

$HOME/mygo

英文:

I am trying to add my project directory to GOPATH, in linux I can do

export GOPATH=$HOME/mygo in ~/.bashrc

what is the equivalence in powershell

答案1

得分: 12

这应该遵循使用Powershell设置任何环境变量的方式(如此文章中所述):

如果您希望它是永久的(即适用于任何未来的shell):

[Environment]::SetEnvironmentVariable("GOPATH", "C:\Your\Path", "User")

需要注意的一点是:当我们使用SetEnvironmentVariable创建一个新的用户级或机器级环境变量时,当我们在Windows PowerShell中运行此命令时,该变量并不总是显示出来:

Get-ChildItem Env:

或者至少在重新启动PowerShell之前(或启动新的PowerShell实例)它不会显示出来。
然而,我们可以随时使用以下命令检索新变量的值:

[Environment]::GetEnvironmentVariable("GOPATH","User")

如果您只想在当前的shell中使用,如cmotley答案中所提到的,并在同一篇文章中详细说明:

$env:GOPATH = "C:\Your\Path"

正如Jaykul所评论的,将该行设置在%UserProfile%\My Documents\WindowsPowerShell\profile.ps1中相当于在~\.bashrc中进行导出操作:
请参阅“Windows PowerShell Profiles”。
(实际上有4个配置文件,提到的一个配置文件仅适用于当前用户,但会影响所有shell)

英文:

This should follow the way you set any Environment variable with Powershell (as described in this article):

If you want it permanent (ie will apply for any future shell):

[Environment]::SetEnvironmentVariable("GOPATH", "C:\Your\Path", "User")

> One thing to watch out for: when we used SetEnvironmentVariable to create a new user- or machine-level environment variable that variable didn’t always show up when we ran this command in Windows PowerShell:

Get-ChildItem Env:

> Or at least it didn’t show up until we restarted PowerShell. (Or started up a new instance of PowerShell.)
However, we could retrieve the value of the new variable at any time by using this command:

[Environment]::GetEnvironmentVariable("GOPATH","User")

If you want it for just the current shell, as mentioned in cmotley's answer and detailed in the same article:

$env:GOPATH = "C:\Your\Path"

As Jaykul comments, setting that line in your %UserProfile%\My Documents\WindowsPowerShell\profile.ps1 is the equivalent of an export in ~\.bashrc:
See "Windows PowerShell Profiles".
(There are actually 4 profiles, the one profile mentioned applies only to the current user, but affects all shells)

答案2

得分: 7

$env:GOPATH = "$HOME\mygo"

英文:

Alternatively, you could do this:

$env:GOPATH = "$HOME\mygo"

huangapple
  • 本文由 发表于 2012年9月26日 00:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/12587253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定