gopls LSP
Go language server integration providing comprehensive code intelligence through gopls (the official Go language server).
Capabilities
- - Code intelligence: Autocomplete, go-to-definition, find references
- Error detection: Real-time diagnostics for compilation errors and issues
- Refactoring: Rename symbols, extract function, organize imports
- Analysis: Static analysis, code suggestions, unused code detection
- Supported extensions: INLINECODE0
Installation
Install gopls using the Go toolchain:
CODEBLOCK0
Important: Make sure $GOPATH/bin (or $HOME/go/bin) is in your PATH.
Verify installation:
CODEBLOCK1
Usage
The language server runs automatically in LSP-compatible editors. For manual operations:
Format code
CODEBLOCK2
Run linter
CODEBLOCK3
Build and test
CODEBLOCK4
Configuration
Create gopls.yaml in your project or workspace for custom settings:
CODEBLOCK5
Or configure via environment:
CODEBLOCK6
Integration Pattern
When editing Go code:
- 1. gopls provides real-time diagnostics in LSP editors
- Run
go fmt or gofmt to format code - Use
go vet for additional static analysis - Run tests with
go test before committing
Common Go Commands
- -
go mod init <module> - Initialize Go module - INLINECODE9 - Clean up dependencies
- INLINECODE10 - Add dependency
- INLINECODE11 - Compile packages
- INLINECODE12 - Run program
- INLINECODE13 - Run tests
- INLINECODE14 - Report suspicious constructs
More Information
gopls LSP
Go语言服务器集成,通过gopls(官方Go语言服务器)提供全面的代码智能支持。
功能特性
- - 代码智能:自动补全、跳转到定义、查找引用
- 错误检测:编译错误和问题的实时诊断
- 重构:重命名符号、提取函数、整理导入
- 分析:静态分析、代码建议、未使用代码检测
- 支持的扩展名:.go
安装
使用Go工具链安装gopls:
bash
go install golang.org/x/tools/gopls@latest
重要提示:确保$GOPATH/bin(或$HOME/go/bin)已添加到PATH环境变量中。
验证安装:
bash
gopls version
使用方法
语言服务器会在支持LSP的编辑器中自动运行。手动操作如下:
格式化代码
bash
gofmt -w file.go
运行代码检查
bash
go vet ./...
构建和测试
bash
go build ./...
go test ./...
配置
在项目或工作区中创建gopls.yaml进行自定义设置:
yaml
analyses:
unusedparams: true
shadow: true
completeUnimported: true
staticcheck: true
或通过环境变量配置:
bash
export GOPLS_CONFIG={staticcheck: true, analyses: {unusedparams: true}}
集成模式
编辑Go代码时:
- 1. gopls在LSP编辑器中提供实时诊断
- 运行go fmt或gofmt格式化代码
- 使用go vet进行额外的静态分析
- 提交前使用go test运行测试
常用Go命令
- - go mod init - 初始化Go模块
- go mod tidy - 清理依赖
- go get - 添加依赖
- go build - 编译包
- go run main.go - 运行程序
- go test - 运行测试
- go vet - 报告可疑结构
更多信息