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的gopls、bingo。
gopls是由Google开发的支持Go的语法特性的Language Server,且支持go Modules因此将使用gopls来作为VSCode的Language Server。
下文将根据下列目录来介绍在VSCode中使用Go:
- 使用VSCode开发go
- Go Tools
- gopls
- go Extensions和gopls 配置
- 要注意的地方
3 使用VSCode开发go
- 在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"
}
- 建立工作空间(WorkSpace)
将项目根目录作为工作空间添加到VSCode(Add workspace folder)。
4 Go Tools
4.1 提供的语法特性
智能感知
- 自动补全(
gocode
) - 方法签名帮助(
gogetdoc
或者godef
+go doc
) - 光标置于代码上时,快速的代码信息提示(
gogetdoc
或者godef
+go doc
)
代码导航
- 跳转或查看符号定义 (
gogetdoc
或godef
+go doc
) - 查找接口实现的符号引用 (
guru
) - 跳转到符号所在文件位置,或者在当前文件中显示符号所在文件符号的大概信息 (
go-outline
) - 在workspace中查找指定符号 (using
go-symbols
) - 绑定Go程序文件和其对应的测试文件
代码编辑
- 快速编码的 代码片段
- Format code on file save as well as format manually (using
goreturns
orgoimports
which also remove unused imports orgofmt
). To disable the format on save feature, add"[go]": {"editor.formatOnSave": false}
to your settings. - Symbol Rename (using
gorename
. Note: For Undo after rename to work in Windows you need to havediff
tool in your path) - Add Imports to current file (using
gopkgs
) - Add/Remove Tags on struct fields (using
gomodifytags
) - Generate method stubs for interfaces (using
impl
) - Fill struct literals with default values (using
fillstruct
)
Diagnostics
- Build-on-save to compile code and show build errors. (using
go build
andgo test
) - Vet-on-save to run
go vet
and show errors as warnings - Lint-on-save to show linting errors as warnings (using
golint
,gometalinter
,staticcheck
,golangci-lint
orrevive
) - Semantic/Syntactic error reporting as you type (using
gotype-live
)
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 (using
gotests
)
Debugging
- Debug your code, binaries or tests (using
delve
)
Others
- Install/Update all dependent Go tools
- Upload to the Go Playground (using
goplay
)
5 gopls
6 go Extensions和gopls 配置
- 1 再VSCode中更新go Extensions和gopls
Command + Shift + P
Input go install
Select -> Go: Install/Update Tools
Select All -> OK
7 要注意的地方
- 如果想使用的扩展工具有单独的path,可以使用go.toolsGopath设置
网友评论