VS Codeのデバッグ実行時に環境変数を設定・エクスポートする方法
|

VS Code でデバッグする際に環境変数を設定し、export する方法のメモです。
.vscode/launch.json の env フィールドを使う方法
.vscode/launch.json
の env フィールドに列挙する。
{
"version": "0.2.0",
"configurations": [
{
"type": "go",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}",
"env": {
"ENV": "local",
"GIN_MODE": "debug",
"MYSQL_HOST": "xxxx",
"MYSQL_DATABASE": "xxxx",
"MYSQL_USER": "xxxx"
}
}
]
}
.vscode/launch.json の envFile フィールドを使う方法
環境変数が多い場合は.env ファイルを読み込むのが楽。
{
"version": "0.2.0",
"configurations": [
{
"type": "go",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env"
}
]
}
グローバルに設定することもできるが、、、
環境変数を設定する際、すべてのアプリケーションにかかるグローバルな形で設定することもできますが、変数名が重複したときに苦労することもあると思うので、↑ のようにアプリケーションごとに適宜設定するのが良さそう。
コメントを残す
comments powered by Disqus