Reputation: 99990
I want to set $GOPATH for each vscode project/workspace. Right now, in .vscode/settings.json, I have:
{
"go.gopath": "$HOME/codes/huru"
}
I close vscode and reopened, and at the command line terminal, I echo $GOPATH, and it's empty. I was hoping that vscode would read the env variable from "go.gopath", but it seems not have to done so.
Does anyone know how to do this?
Upvotes: 7
Views: 37676
Reputation: 11502
The go.gopath
on user settings or workspace settings will replace the GOPATH value on the VSCode. This particular GOPATH value is the one that shows up whenever Go: Current GOPATH
command is executed on VSCode, so it is not the $GOPATH
environment variable.
Explanation from GOPATH in the VS Code Go extension:
By default, the extension uses the value of the environment variable GOPATH. If no such environment variable is set, the extension runs go env and uses the GOPATH reported by the go command.
Note that, much like a
PATH
variable, GOPATH can contain multiple directory paths, separated by:
or;
. This allows you to set different GOPATHs for different projects.Setting
go.gopath
in your user settings overrides the environment's GOPATH value.Workspace settings override user settings, so you can use the go.gopath setting to set different GOPATHs for different projects. A GOPATH can also contain multiple directories, so this setting is not necessary to achieve this behavior.
Below is an example I've made up that might be useful on trying to understand the differences.
$GOPATH
env on my local with a certain value. Then I set the go.gopath
on the user settings (with different value compared to the $GOPATH
). When I execute the command Go: Current GOPATH
, a popup on the bottom right appears, showing the very same value as on my go.gopath
settings. I put red line on all of this.echo $GOPATH
on the terminal, the output is still the $GOPATH
value from my env variable (blue line). This is because go.gopath
setting will not replace the $GOPATH
env variable.In your case, the echo $GOPATH
return empty output because you haven't set the $GOPATH
environment variable.
Upvotes: 8