Go
Zed 原生支持 Go。
- Tree-sitter: tree-sitter/tree-sitter-go
- 语言服务器: golang/tools/tree/master/gopls
- 调试适配器: delve
设置
我们建议通过 Go 的包管理器安装 gopls,而不是通过 Homebrew 或您的 Linux 发行版的包管理器。
- 请确保您已卸载通过包管理器安装的任何版本的 gopls
# MacOS homebrew
brew remove gopls
# Ubuntu
sudo apt-get remove gopls
sudo snap remove gopls
# Arch
sudo pacman -R gopls
- 使用 Go 模块工具安装/更新
gopls到最新版本
go install golang.org/x/tools/gopls@latest
- 确保
gopls在您的路径中
which gopls
gopls version
如果找不到 gopls,您可能需要将 export PATH="$PATH:$HOME/go/bin" 添加到您的 .zshrc / .bash_profile
内联提示
Zed 为内联提示设置了以下初始化选项
"hints": {
"assignVariableTypes": true,
"compositeLiteralFields": true,
"compositeLiteralTypes": true,
"constantValues": true,
"functionTypeParameters": true,
"parameterNames": true,
"rangeVariableTypes": true
}
以使语言服务器在 Zed 在设置中启用它们时返回内联提示。
使用
"lsp": {
"gopls": {
"initialization_options": {
"hints": {
// ....
}
}
}
}
覆盖这些设置。
有关更多信息,请参阅 gopls 内联提示文档。
调试
Zed 支持使用 Delve 对 Go 测试和入口点(func main)进行零配置调试。运行 debugger: start (f4|f4) 以查看这些预配置调试任务的上下文列表。
为了进行更多控制,您可以将调试配置添加到 .zed/debug.json。请参阅下面的示例。
调试 Go 包
要调试特定包,您可以将 Delve 模式设置为 "debug"。在这种情况下,"program" 应该设置为包名。
[
{
"label": "Go (Delve)",
"adapter": "Delve",
"program": "$ZED_FILE",
"request": "launch",
"mode": "debug"
},
{
"label": "Run server",
"adapter": "Delve",
"request": "launch",
"mode": "debug",
// For Delve, the program can be a package name
"program": "./cmd/server"
// "args": [],
// "buildFlags": [],
}
]
调试 Go 测试
要调试包的测试,请将 Delve 模式设置为 "test"。"program" 仍然是包名,您可以使用 "buildFlags" 进行设置标签等操作,并使用 "args" 在测试二进制文件上设置参数。(有关更多信息,请参阅 go help testflags)。
[
{
"label": "Run integration tests",
"adapter": "Delve",
"request": "launch",
"mode": "test",
"program": ".",
"buildFlags": ["-tags", "integration"]
// To filter down to just the test your cursor is in:
// "args": ["-test.run", "$ZED_SYMBOL"]
}
]
单独构建和调试
如果您需要使用特定命令构建应用程序,您可以使用 Delve 的 "exec" 模式。在这种情况下,"program" 应该指向一个可执行文件,并且 "build" 命令应该构建该文件。
[
{
"label": "Debug Prebuilt Unit Tests",
"adapter": "Delve",
"request": "launch",
"mode": "exec",
"program": "${ZED_WORKTREE_ROOT}/__debug_unit",
"args": ["-test.v", "-test.run=${ZED_SYMBOL}"],
"build": {
"command": "go",
"args": [
"test",
"-c",
"-tags",
"unit",
"-gcflags\"all=-N -l\"",
"-o",
"__debug_unit",
"./pkg/..."
]
}
}
]
附加到 Delve 的现有实例
您可能需要连接到 Delve 的现有实例,该实例不一定在您的机器上运行;在这种情况下,您可以使用 tcp_arguments 来配置 Zed 与 Delve 的连接。
[
{
"adapter": "Delve",
"label": "Connect to a running Delve instance",
"program": "/Users/zed/Projects/language_repositories/golang/hello/hello",
"cwd": "/Users/zed/Projects/language_repositories/golang/hello",
"args": [],
"env": {},
"request": "launch",
"mode": "exec",
"stopOnEntry": false,
"tcp_connection": { "host": "127.0.0.1", "port": 53412 }
}
]
在这种情况下,Zed 不会生成 Delve 的新实例,因为它选择使用现有实例。这样做的结果是 Zed 中将没有终端;您必须直接与 Delve 实例交互,因为它处理调试对象的 stdin/stdout。
Go Mod
- Tree-sitter: camdencheek/tree-sitter-go-mod
- 语言服务器: 不适用
Go Sum
- Tree-sitter: amaanq/tree-sitter-go-sum
- 语言服务器: 不适用
Go Work
- Tree-sitter: tree-sitter-go-work
- 语言服务器: 不适用