1# Go
2
3Go support is available natively in Zed.
4
5- Tree-sitter: [tree-sitter/tree-sitter-go](https://github.com/tree-sitter/tree-sitter-go)
6- Language Server: [golang/tools/tree/master/gopls](https://github.com/golang/tools/tree/master/gopls)
7- Debug Adapter: [delve](https://github.com/go-delve/delve)
8
9## Setup
10
11We recommend installing gopls via go's package manager and not via Homebrew or your Linux distribution's package manager.
12
131. Make sure you have uninstalled any version of gopls you have installed via your package manager:
14
15```sh
16# MacOS homebrew
17brew remove gopls
18# Ubuntu
19sudo apt-get remove gopls
20sudo snap remove gopls
21# Arch
22sudo pacman -R gopls
23```
24
252. Install/Update `gopls` to the latest version using the go module tool:
26
27```sh
28go install golang.org/x/tools/gopls@latest
29```
30
313. Ensure that `gopls` is in your path:
32
33```sh
34which gopls
35gopls version
36```
37
38If `gopls` is not found you will likely need to add `export PATH="$PATH:$HOME/go/bin"` to your `.zshrc` / `.bash_profile`
39
40## Inlay Hints
41
42Zed sets the following initialization options for inlay hints:
43
44```json [settings]
45"hints": {
46 "assignVariableTypes": true,
47 "compositeLiteralFields": true,
48 "compositeLiteralTypes": true,
49 "constantValues": true,
50 "functionTypeParameters": true,
51 "parameterNames": true,
52 "rangeVariableTypes": true
53}
54```
55
56to make the language server send back inlay hints when Zed has them enabled in the settings.
57
58Use
59
60```json [settings]
61"lsp": {
62 "gopls": {
63 "initialization_options": {
64 "hints": {
65 // ....
66 }
67 }
68 }
69}
70```
71
72to override these settings.
73
74See [gopls inlayHints documentation](https://github.com/golang/tools/blob/master/gopls/doc/inlayHints.md) for more information.
75
76## Debugging
77
78Zed supports zero-configuration debugging of Go tests and entry points (`func main`) using Delve. Run {#action debugger::Start} ({#kb debugger::Start}) to see a contextual list of these preconfigured debug tasks.
79
80For more control, you can add debug configurations to `.zed/debug.json`. See below for examples.
81
82- [Delve configuration documentation](https://github.com/go-delve/delve/blob/master/Documentation/api/dap/README.md#launch-and-attach-configurations)
83
84### Debug Go Packages
85
86To debug a specific package, you can do so by setting the Delve mode to "debug". In this case "program" should be set to the package name.
87
88```json [debug]
89[
90 {
91 "label": "Go (Delve)",
92 "adapter": "Delve",
93 "program": "$ZED_FILE",
94 "request": "launch",
95 "mode": "debug"
96 },
97 {
98 "label": "Run server",
99 "adapter": "Delve",
100 "request": "launch",
101 "mode": "debug",
102 // For Delve, the program can be a package name
103 "program": "./cmd/server"
104 // "args": [],
105 // "buildFlags": [],
106 }
107]
108```
109
110### Debug Go Tests
111
112To debug the tests for a package, set the Delve mode to "test".
113The "program" is still the package name, and you can use the "buildFlags" to do things like set tags, and the "args" to set args on the test binary. (See `go help testflags` for more information on doing that).
114
115```json [debug]
116[
117 {
118 "label": "Run integration tests",
119 "adapter": "Delve",
120 "request": "launch",
121 "mode": "test",
122 "program": ".",
123 "buildFlags": ["-tags", "integration"]
124 // To filter down to just the test your cursor is in:
125 // "args": ["-test.run", "$ZED_SYMBOL"]
126 }
127]
128```
129
130### Build and debug separately
131
132If you need to build your application with a specific command, you can use the "exec" mode of Delve. In this case "program" should point to an executable,
133and the "build" command should build that.
134
135```json [debug]
136[
137 {
138 "label": "Debug Prebuilt Unit Tests",
139 "adapter": "Delve",
140 "request": "launch",
141 "mode": "exec",
142 "program": "${ZED_WORKTREE_ROOT}/__debug_unit",
143 "args": ["-test.v", "-test.run=${ZED_SYMBOL}"],
144 "build": {
145 "command": "go",
146 "args": [
147 "test",
148 "-c",
149 "-tags",
150 "unit",
151 "-gcflags\"all=-N -l\"",
152 "-o",
153 "__debug_unit",
154 "./pkg/..."
155 ]
156 }
157 }
158]
159```
160
161### Attaching to an existing instance of Delve
162
163You might find yourself needing to connect to an existing instance of Delve that's not necessarily running on your machine; in such case, you can use `tcp_arguments` to instrument Zed's connection to Delve.
164
165```json [debug]
166[
167 {
168 "adapter": "Delve",
169 "label": "Connect to a running Delve instance",
170 "program": "/Users/zed/Projects/language_repositories/golang/hello/hello",
171 "cwd": "/Users/zed/Projects/language_repositories/golang/hello",
172 "args": [],
173 "env": {},
174 "request": "launch",
175 "mode": "exec",
176 "stopOnEntry": false,
177 "tcp_connection": { "host": "127.0.0.1", "port": 53412 }
178 }
179]
180```
181
182In such case Zed won't spawn a new instance of Delve, as it opts to use an existing one. The consequence of this is that _there will be no terminal_ in Zed; you have to interact with the Delve instance directly, as it handles stdin/stdout of the debuggee.
183
184## Go Mod
185
186- Tree-sitter: [camdencheek/tree-sitter-go-mod](https://github.com/camdencheek/tree-sitter-go-mod)
187- Language Server: N/A
188
189## Go Sum
190
191- Tree-sitter: [amaanq/tree-sitter-go-sum](https://github.com/amaanq/tree-sitter-go-sum)
192- Language Server: N/A
193
194## Go Work
195
196- Tree-sitter:
197 [tree-sitter-go-work](https://github.com/d1y/tree-sitter-go-work)
198- Language Server: N/A