vscode

Go for VSCode

2019-10-25  本文已影响0人  lkzy

1. 相关文档文档

主文档
https://github.com/microsoft/vscode-go
扩展工具(extension tools)的介绍和下载方式
https://github.com/Microsoft/vscode-go/wiki/Go-tools-that-the-Go-extension-depends-on
gopls文档
https://github.com/golang/tools/blob/master/gopls/doc/user.md
在VSCode中使用Go的常见问题和解决方式
https://github.com/Microsoft/vscode-go/wiki/Go-with-VS-Code-FAQ-and-Troubleshooting

2 介绍

VSCode使用Go Extensions来为Go的开发提供了丰富的语言支持。

Go Extensions通过使用一组Go Tools来支持相应的语言特性。

由于Go Extensions使用了大量的Go Tools来提供语法特性,因此为了简化大量Go Tools依赖带来的麻烦,使用了单个语法服务来替换这些Go Tools,这个语法服务由 Language Server Protocol(简称LSP)来提供标准,具体实现有Google的goplsbingo

gopls是由Google开发的支持Go的语法特性的Language Server,且支持go Modules因此将使用gopls来作为VSCode的Language Server。

下文将根据下列目录来介绍在VSCode中使用Go:

3 使用VSCode开发go

  1. 在VSCode的Extensions中添加Go插件
    2.在File->Preferences->settings->Extensions->Go Configuration中打开setting.json,写入如下配置
{
    "go.useLanguageServer": true,//使用gopls
    "go.languageServerExperimentalFeatures": {
        "diagnostics": true // for diagnostics as you type
    },
    "[go]": {
        "editor.snippetSuggestions": "none",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
    },
    "gopls": {
        "usePlaceholders": true // add parameter placeholders when completing a function
    },
    //"go.languageServerFlags": ["-logfile", "D:/gopls.txt","-rpc.trace"],//gopls日志trace跟踪与文件记录
    "files.eol": "\n"
}
  1. 建立工作空间(WorkSpace)
    将项目根目录作为工作空间添加到VSCode(Add workspace folder)。

4 Go Tools

4.1 提供的语法特性

智能感知
代码导航

代码编辑

Diagnostics

Testing

Debugging

Others

5 gopls

6 go Extensions和gopls 配置

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

7 要注意的地方

  1. 如果想使用的扩展工具有单独的path,可以使用go.toolsGopath设置
上一篇 下一篇

猜你喜欢

热点阅读