DEV Community

Vuong
Vuong

Posted on • Updated on

Go LSP in Sublime Text

Here is how to configure LSP (language server protocol) for Go development in Sublime Text.

Prerequisites

  • go
  • gopls
  • gotools (gofmt, goimports...)

Packages

  • sublimelsp/LSP

LSP settings

Open LSP Settings, then paste the below one

// for MacOS

{
    "clients":
    {
        "gopls":
        {
            "command": [
                "<path_to>/go/bin/gopls",
                "-v",
                "-rpc.trace",
                "-logfile=<path_to>/gopls.log"
            ],
            "enabled": true,
            "env": {
                "PATH": "<echo $PATH in terminal then paste its value here>"
            },
            "scopes":["source.go"],
            "syntaxes": ["Packages/Go/Go.sublime-syntax"],
            "settings": {
                "gopls.usePlaceholders": true,
                "gopls.completeUnimported": true,
            },
            "languageId": "go"

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

// for Windows

{
    "clients":
    {
        "gopls":
        {
            "command": [
                "<path_to>\\bin\\gopls.EXE",
                "-v",
                "-rpc.trace",
                "-logfile=<path_to>\\gopls.log"
            ],
            "enabled": true,
            "scopes":["source.go"],
            "syntaxes": ["Packages/Go/Go.sublime-syntax"],
            "settings": {
                "gopls.usePlaceholders": true,
                "gopls.completeUnimported": true,
            },
            "languageId": "go"
        },
    }
}
Enter fullscreen mode Exit fullscreen mode

LSP Key Binding

Search LSP Key Binding then paste the below value into the field

[
    // Go To Definition
    {
        "command": "lsp_symbol_definition",
        "args": {
            "side_by_side": false
        },
        "keys": [
            "ctrl+d"
        ],
        "context": [
            {
                "key": "lsp.session_with_capability",
                "operator": "equal",
                "operand": "definitionProvider"
            },
            {
                "key": "auto_complete_visible",
                "operator": "equal",
                "operand": false
            }
        ]
    },
]

Enter fullscreen mode Exit fullscreen mode

Known Issues

  • Auto-format on macOS doesn't work but works well on Windows

=====

Updated: This package is now working well with simple set up. https://packagecontrol.io/packages/LSP-gopls

Gofumpt for auto-format also works: https://github.com/mvdan/gofumpt#sublime-text

Top comments (0)