VSCode Go 插件环境通常依赖一些 Go tools.

插件环境提供了以下语言特性:

  • IntelliSense
    • Auto Completion of symbols as you type
    • Signature Help for functions as you type
    • Quick Info on the symbol as you hover over it
  • Code Navigation
    • Go to or Peek Definition of symbols
    • Find References of symbols and Implementations of interfaces
    • Go to symbol in file or see the file outline
    • Go to symbol in workspace
    • Toggle between a Go program and the corresponding test file
  • Code Editing
    • Code Snippets for quick coding
    • Format code on file save as well as format manually. To disable the format on save - feature, add “[go]”: {“editor.formatOnSave”: false} to your settings.
    • Symbol Rename
    • Add Imports to current file
    • Add/Remove Tags on struct fields
    • Generate method stubs for interfaces
    • Fill struct literals with default values
  • Diagnostics
    • Build-on-save to compile code and show build errors
    • Vet-on-save to run go vet and show errors as warnings
    • Lint-on-save to show linting errors as warnings
    • Semantic/Syntactic error reporting as you type
  • Testing
    • Run Tests under the cursor, in current file, in current package, in the whole workspace using either commands or codelens
    • Run Benchmarks under the cursor using either commands or codelens
    • Show code coverage either on demand or after running tests in the package
    • Generate unit tests skeleton
  • Debugging
    • Debug your code, binaries or tests
  • Others
    • Install/Update all dependent Go tools
    • Upload to the Go Playground

若使用 Go language server, 则不再依赖大部分的 tools, 那些特性将由 Go language server 提供.

如果你在使用 Go modules 管理模块, 强烈建议使用 language server.

1
2
3
Below are the tools essential for the general features of this extension. If you have chosen to use the Go language server, then most of the below tools are no longer needed as the corresponding features will be provided by the language server. Eventually, once the language server is stable, we will move to using it and deprecate the use of individual tools below.

NOTE: If you are using Go modules, then we strongly recommend using the Go language server as it performs much better than the tools below.

What is LSP

The LSP allows any text editor to be extended with IDE-like features

The Language Server protocol is used between a tool (the client) and a language smartness provider (the server) to integrate features like auto complete, go to definition, find all references and alike into the tool

how lsp works

Go language serverLSP(Language Server Protocol) 的一种, 官方实现是 gopls, sourcegraph 也实现了一个 Go language server 名为 go-langserver.

VSCode 推荐使用 gopls.

1
2
3
Previously, we added support to use go-langserver, the language server from Sourcegraph. There is no active development for it anymore and it doesn't support Go modules. Therefore, we are now switching to use gopls, the language server from Google which is currently in active development.

If you are already using the language server from Sourcegraph, you can continue to use it as long as you are not using Go modules. We do suggest you to move to using gopls though.

Microsoft 维护了一个 language server list.

language server 相当于服务端, 编辑器(如 vscode)是客户端. 客户端与服务端之间通过 LSP 进行数据交互.

安装 Go Language Server

go get golang.org/x/tools/gopls@latest

配置 Go Language Server

VSCode settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    "go.useLanguageServer": true,
    "[go]": {
        "editor.snippetSuggestions": "none",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    },
    "go.languageServerExperimentalFeatures": {
        "format": false,
        "autoComplete": true,
        "rename": true,
        "goToDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "goToTypeDefinition": true,
        "goToImplementation": true,
        "documentSymbols": true,
        "workspaceSymbols": true,
        "findReferences": true,
        "diagnostics": false
    },
    "go.languageServerFlags": [
        "-rpc.trace", // for more detailed debug logging
        "serve",
        "--debug=localhost:6060", // to investigate memory usage, see profiles 
    ],
    "files.eol": "\n", // formatting only supports LF line endings
    "go.goroot": "/usr/local/go",
    "go.gopath": "~/go"

异常

The gopls server crashed 5 times in the last 3 minutes. The server will not be restarted.

类似问题:

解决方案

升级 gopls

go get -u golang.org/x/tools/cmd/gopls

  • Command + Shift + P
  • Input go install
  • Select -> Go: Install/Update Tools
  • Select All -> OK

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    Installing 18 tools at ~/go/bin
    gocode
    gopkgs
    go-outline
    go-symbols
    guru
    gorename
    dlv
    gocode-gomod
    godef
    goreturns
    golint
    gopls
    gotests
    gomodifytags
    impl
    fillstruct
    goplay
    godoctor
    
    Installing github.com/mdempsky/gocode SUCCEEDED
    Installing github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDED
    Installing github.com/ramya-rao-a/go-outline SUCCEEDED
    Installing github.com/acroca/go-symbols SUCCEEDED
    Installing golang.org/x/tools/cmd/guru SUCCEEDED
    Installing golang.org/x/tools/cmd/gorename SUCCEEDED
    Installing github.com/go-delve/delve/cmd/dlv SUCCEEDED
    Installing github.com/stamblerre/gocode SUCCEEDED
    Installing github.com/rogpeppe/godef SUCCEEDED
    Installing github.com/sqs/goreturns SUCCEEDED
    Installing golang.org/x/lint/golint SUCCEEDED
    Installing golang.org/x/tools/cmd/gopls SUCCEEDED
    Installing github.com/cweill/gotests/... SUCCEEDED
    Installing github.com/fatih/gomodifytags SUCCEEDED
    Installing github.com/josharian/impl SUCCEEDED
    Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED
    Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED
    Installing github.com/godoctor/godoctor SUCCEEDED
    
    Reload VS Code window to use the Go language server
    All tools successfully installed. You're ready to Go :).

Refs