Go Development: VS Code Workspace

If you’re diving into Go development, understanding workspace in VS Code is like finding the perfect playlist for your coding sessions.

How often do you find yourself stuck in settings hell and getting error like
Let’s break it down.

Refresher: What’s CWD and Workspace?

Current Working Directory (CWD): Think of the CWD as your code’s current address. It affects how your code finds files. If your Go code looks for a file in the same folder, it’s searching within the CWD.

Workspace: The workspace is like your project’s home. It’s where all your important files live. In Go, setting the workspace is key, especially in bigger projects.

Setting VSCode Workspace

How to:

  1. Open your Go project folder in VS Code.
  2. Navigate to “File” > “Add Folder to Workspace…”.
  3. Select your project directory.
  4. Use “.” in your launch settings (.vscode/launch.json)to align your workspace as the CWD.

When to Use:

  • Perfect for a smooth ride within VS Code.
  • Ideal for running and debugging your code directly in VS Code.
  • Great if your project structure mirrors the VS Code workspace setup.

Example - Default Behavior: If your main.go is in a folder named my_project, Go instinctively looks in my_project for files. Using "." in VS Code settings to set the CWD as my_project isn't mandatory .

this is how default “.vscode/launch.json” might look like

{
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${file}",
"cwd": "${workspaceFolder}" // defaults to the directory of "${file}"
}
]
}

i.e. Configuration is set to launch the current file using Delve in debug mode. This is the reason, when someone is having a single file code or simple flat structure, debugging works just fine without setting anything :)

Example — Setting relative path : Consider Achilles -https://github.com/chilwaltanuj/achilles
As you can see this project does not have main.go file in root folder, hence, we need to adjust the setting to let VS code know the same. Set the values as below.

{
"configurations": [
{
"name": "debug http/main.go",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/entrypoint/http/main.go",
"cwd": "${workspaceFolder}" // Setting current working directory to the root of the project
}
]
}

Above example dictates to launch a debug session for go type application, having entry point as file “ root folder/entrypoint/http/main.go”.

It will look like this in VS code.

attributes :

  • configurations: An array containing configurations for debugging. In this case, there’s one configuration specified.
  • name: The name of this configuration, displayed in the VS Code dropdown. This one is named “debug http/main.go.”
  • type: Specifies the type of debugger to use. For Go projects, it should be set to “go.”
  • request: Defines the type of request this configuration makes. In this context, it’s set to “launch” because we are launching the program.
  • mode: Specific to the Go extension for VS Code, it defines the debugging mode. In this case, “auto” means the debugger will automatically decide whether to attach to an existing process or launch a new one.
  • program: Specifies the main program to be run or debugged. It’s the entry point of your application. In this example, it’s set to the Go program located at ${workspaceFolder}/entrypoint/http/main.go.
  • cwd (current working directory): Specifies the current working directory for the launched program. This is where your program will look for files unless specified otherwise. It’s set to ${workspaceFolder}, ensuring the current working directory is the root of your project.

Conclusion

Mastering your workspace game in Go is a game-changer for beginners. Whether you’re comfortable in the VS Code environment or just starting to explore, aligning your workspace is a small tweak that makes a big impact.
If you still facing any issue on debugging or need more insights check this to resolve it.

Feel free to share your thoughts in the comments below! If you found this guide to be a helpful companion, pass it along to your fellow gophers! 🚀
Keep it simple, stay efficient, and happy coding! #fellowGopher”