Environment setup for developing golang/go

241 views
Skip to first unread message

Francesco Mari

unread,
Feb 24, 2022, 7:20:17 PM2/24/22
to golang-nuts
Hi all,

I wanted to dig deeper into Go and start exploring the toolchain and the standard library. I cloned github.com/golang/go and noticed that the src/cmd directory contains a go.mod. I opened my editor (Visual Studio Code + Go plugin) in the src/cmd directory. I expected to be able to navigate the code like I normally do, but something about that project makes my editor unhappy.

I assume that there are people here that already have their favourite editor/IDE set up to contribute to the language itself. Can you please share your setup? Are there any tips and tricks you can share to have code navigation and autocompletion working in golang/go?

Zik Aeroh

unread,
Feb 24, 2022, 7:45:15 PM2/24/22
to golang-nuts
The way I do it in VS Code is to do something like this from the repo root:

# Get the dev version of Go into PATH for the editor and local dev work
$ export PATH=$PWD/bin:$PATH
# Build a baseline version of go and save it to restore if things break, or for comparison
$ (cd src; GOGC=off ./make.bash) && toolstash save
# Launch VS Code with the relevant module roots, with PATH set
$ code ../go.code-workspace

Where go.code-workspace is in the folder above my checkout (as it's not in .gitignore):

{
    "folders": [
                {
                        "path": "go/src"
                },
                {
                        "path": "go/src/cmd"
                },
                {
                        "path": "go/test"
                }
        ],
    "settings": {
                "editor.codeActionsOnSave": {"source.fixAll": false},
                "go.lintOnSave": "off",
                "go.lintTool": "staticcheck",
                "gopls": {
                        // "ui.semanticTokens": true,
                        "ui.diagnostic.analyses": {
                                "unsafeptr": false,
                                "simplifycompositelit": false,
                        },
                        "ui.diagnostic.staticcheck": false,
                },
                "go.languageServerFlags": [
                        "-remote=auto;godev",
                ],
        }
}

Which defines explicitly marks those roots for gopls, and sets a couple of settings that make editing the toolchain a little less annoying (thanks to the extra diagnostics and fixes and such).

It's entirely possible that there's an easier way to do this, or that it's already moot thanks to experimental multi-module stuff / workfile stuff. That'd be nice to know (so I can remove all of this), but for now this is what I do and I'm relatively content

Francesco Mari

unread,
Feb 25, 2022, 8:56:46 AM2/25/22
to golang-nuts
Thanks for sharing this! Picking up on that configuration, I found this piece of documentation about gopls [1]. I followed these steps:
  • Check out github.com/golang/go to /Users/frm/src/go.
  • Build the language distribution as described here [2].
  • Create a new VSCode workspace at /Users/frm/src/go.code-workspace (shown below).
  • Add the necessary directories and configuration to the workspace.
  • Restart the language server.
  • Reinstall/Update all the tools.
This is the workspace configuration. I wanted to use the minimum amount of configuration, so I could understand what the right options really are.

{
"folders": [
{
"path": "go/src/cmd"
},
{
"path": "go/test"
},
{
"path": "go/src"
}
],
"settings": {
"go.goroot": "/Users/frm/src/go"
}
}

After following the steps above, I managed to have navigation and code completion in my editor.

Reply all
Reply to author
Forward
0 new messages