| 1 | --- |
| 2 | title: "Gopls: Navigation features" |
| 3 | --- |
| 4 | |
| 5 | This page documents gopls features for navigating your source code. |
| 6 | |
| 7 | <!-- TODO: screenshots --> |
| 8 | |
| 9 | ## Definition |
| 10 | |
| 11 | The LSP [`textDocument/definition`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_definition) |
| 12 | request returns the location of the declaration of the symbol under the cursor. |
| 13 | Most editors provide a command to navigate directly to that location. |
| 14 | |
| 15 | A definition query also works in these unexpected places: |
| 16 | |
| 17 | - On an **import path**, it returns the list of locations, of |
| 18 | each package declaration in the files of the imported package. |
| 19 | - On a **package declaration**, it returns the location of |
| 20 | the package declaration that provides the documentation of that package. |
| 21 | - On a symbol in a **[`go:linkname` directive](https://pkg.go.dev/cmd/compile)**, |
| 22 | it returns the location of that symbol's declaration. |
| 23 | - On a **[doc link](https://tip.golang.org/doc/comment#doclinks)**, it returns |
| 24 | (like [`hover`](passive.md#hover)) the location of the linked symbol. |
| 25 | - On a file name in a **[`go:embed` directive](https://pkg.go.dev/embed)**, |
| 26 | it returns the location of the embedded file. |
| 27 | - On the declaration of a non-Go function (a `func` with no body), |
| 28 | it returns the location of the assembly implementation, if any, |
| 29 | - On a **return statement**, it returns the location of the function's result variables. |
| 30 | - On a **goto**, **break**, or **continue** statement, it returns the |
| 31 | location of the label, the closing brace of the relevant block statement, or the |
| 32 | start of the relevant loop, respectively. |
| 33 | |
| 34 | <!-- On a built-in symbol such as `append` or `unsafe.Pointer`, `definition` reports |
| 35 | the location of the declaration in the builtin or unsafe pseudo-packages, |
| 36 | which are just documentation. --> |
| 37 | |
| 38 | Client support: |
| 39 | - **VS Code**: Use [Go to Definition](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-definition) (`F12` or `⌘`-click). |
| 40 | If the cursor is already at the declaration, the request is instead interpreted as "Go to References". |
| 41 | - **Emacs + eglot**: use [`M-x xref-find-definitions`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Xref.html). |
| 42 | - **Vim + coc.nvim**: ?? |
| 43 | - **CLI**: `gopls definition file.go:#offset` |
| 44 | |
| 45 | ## References |
| 46 | |
| 47 | The LSP [`textDocument/references`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_references) |
| 48 | request returns the locations of all identifiers that refer to the symbol under the cursor. |
| 49 | |
| 50 | The references algorithm handles various parts of syntax as follows: |
| 51 | |
| 52 | - The references to a **symbol** report all uses of that symbol. |
| 53 | In the case of exported symbols this may include locations in other packages. |
| 54 | - The references to a **package declaration** are all the |
| 55 | direct imports of the package, along with all the other package |
| 56 | declarations in the same package. |
| 57 | - It is an error to request the references to a **built-in symbol** |
| 58 | such as `int` or `append`, |
| 59 | as they are presumed too numerous to be of interest. |
| 60 | - The references to an **interface method** include references to |
| 61 | concrete types that implement the interface. Similarly, the |
| 62 | references to a **method of a concrete type** include references to |
| 63 | corresponding interface methods. |
| 64 | - An **embedded field** `T` in a struct type such as `struct{T}` is |
| 65 | unique in Go in that it is both a reference (to a type) and a |
| 66 | definition (of a field). |
| 67 | The `references` operation reports only the references to it [as a field](https://go.dev/issue/63521). |
| 68 | To find references to the type, jump to the type declararation first. |
| 69 | - The references to a module in a **require** directive in a **go.mod** file are |
| 70 | the **import** statements in packages of the main module (defined by the go.mod file) that |
| 71 | import packages of the required module. |
| 72 | |
| 73 | Be aware that a references query returns information only about the |
| 74 | build configuration used to analyze the selected file, so if you ask |
| 75 | for the references to a symbol defined in `foo_windows.go`, the result |
| 76 | will never include the file `bar_linux.go`, even if that file refers |
| 77 | to a symbol of the same name; see https://go.dev/issue/65755. |
| 78 | |
| 79 | Clients can request that the declaration be included among the |
| 80 | references; most do. |
| 81 | |
| 82 | Client support: |
| 83 | - **VS Code**: Use [`Go to References`](https://code.visualstudio.com/docs/editor/editingevolved#_peek) to quickly "peek" at the references, |
| 84 | or `Find all References` to open the references panel. |
| 85 | - **Emacs + eglot**: Via [`xref` package](https://www.gnu.org/software/emacs/manual/html_node/emacs/Xref.html): use `M-x xref-find-references`. |
| 86 | - **Vim + coc.nvim**: ?? |
| 87 | - **CLI**: `gopls references file.go:#offset` |
| 88 | |
| 89 | ## Implementation |
| 90 | |
| 91 | The LSP |
| 92 | [`textDocument/implementation`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_implementation) |
| 93 | request queries the relation between abstract and concrete types and |
| 94 | their methods. |
| 95 | |
| 96 | Interfaces and concrete types are matched using method sets: |
| 97 | |
| 98 | - When invoked on a reference to an **interface type**, it returns the |
| 99 | location of the declaration of each type that implements |
| 100 | the interface. |
| 101 | - When invoked on a **concrete type**, |
| 102 | it returns the locations of the matching interface types. |
| 103 | - When invoked on an **interface method**, it returns the corresponding |
| 104 | methods of the types that satisfy the interface. |
| 105 | - When invoked on a **concrete method**, |
| 106 | it returns the locations of the matching interface methods. |
| 107 | |
| 108 | For example: |
| 109 | - `implementation(io.Reader)` includes subinterfaces such as `io.ReadCloser`, |
| 110 | and concrete implementations such as `*os.File`. It also includes |
| 111 | other declarations equivalent to `io.Reader`. |
| 112 | - `implementation(os.File)` includes only interfaces, such as |
| 113 | `io.Reader` and `io.ReadCloser`. |
| 114 | |
| 115 | The LSP's Implementation feature has a built-in bias towards subtypes, |
| 116 | possibly because in languages such as Java and C++ the relationship |
| 117 | between a type and its supertypes is explicit in the syntax, so the |
| 118 | corresponding "Go to interfaces" operation can be achieved as sequence |
| 119 | of two or more "Go to definition" steps: the first to visit the type |
| 120 | declaration, and the rest to sequentially visit ancestors. |
| 121 | (See https://github.com/microsoft/language-server-protocol/issues/2037.) |
| 122 | |
| 123 | In Go, where there is no syntactic relationship between two types, a |
| 124 | search is required when navigating in either direction between |
| 125 | subtypes and supertypes. The heuristic above works well in many cases, |
| 126 | but it is not possible to ask for the superinterfaces of |
| 127 | `io.ReadCloser`. For more explicit navigation between subtypes and |
| 128 | supertypes, use the [Type Hierarchy](#Type Hierarchy) feature. |
| 129 | |
| 130 | Only non-trivial interfaces are considered; no implementations are |
| 131 | reported for type `any`. |
| 132 | |
| 133 | Within the same package, all matching types/methods are reported. |
| 134 | However, across packages, only exported package-level types and their |
| 135 | methods are reported, so local types (whether interfaces, or struct |
| 136 | types with methods due to embedding) may be missing from the results. |
| 137 | <!-- Reason: assignability of local types such as I and J defined thus: |
| 138 | package p; func P() {type I interface {...}} |
| 139 | package q; func Q() {type J struct {...}} |
| 140 | depends on them both being in the same types.Importer "realm", |
| 141 | but that is not consistent with the "scalable" gopls design. |
| 142 | --> |
| 143 | |
| 144 | Functions, `func` types, and dynamic function calls are matched using signatures: |
| 145 | |
| 146 | - When invoked on the `func` token of a **function definition**, |
| 147 | it returns the locations of the matching signature types |
| 148 | and dynamic call expressions. |
| 149 | - When invoked on the `func` token of a **signature type**, |
| 150 | it returns the locations of the matching concrete function definitions. |
| 151 | - When invoked on the `(` token of a **dynamic function call**, |
| 152 | it returns the locations of the matching concrete function |
| 153 | definitions. |
| 154 | |
| 155 | If either the target type or the candidate type are generic, the |
| 156 | results will include the candidate type if there is any instantiation |
| 157 | of the two types that would allow one to implement the other. |
| 158 | (Note: the matcher doesn't current implement full unification, so type |
| 159 | parameters are treated like wildcards that may match arbitrary |
| 160 | types, without regard to consistency of substitutions across the |
| 161 | method set or even within a single method. |
| 162 | This may lead to occasional spurious matches.) |
| 163 | |
| 164 | Since a type may be both a function type and a named type with methods |
| 165 | (for example, `http.HandlerFunc`), it may participate in both kinds of |
| 166 | implementation queries (by method-sets and function signatures). |
| 167 | Queries using method-sets should be invoked on the type or method name, |
| 168 | and queries using signatures should be invoked on a `func` or `(` token. |
| 169 | |
| 170 | Client support: |
| 171 | - **VS Code**: Use [Go to Implementations](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-implementation) (`⌘F12`). |
| 172 | - **Emacs + eglot**: Use `M-x eglot-find-implementation`. |
| 173 | - **Vim + coc.nvim**: ?? |
| 174 | - **CLI**: `gopls implementation file.go:#offset` |
| 175 | |
| 176 | |
| 177 | ## Type Definition |
| 178 | |
| 179 | The LSP |
| 180 | [`textDocument/typeDefinition`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_typeDefinition) |
| 181 | request returns the location of the type of the selected symbol. |
| 182 | |
| 183 | For example, if the selection is the name `buf` of a local variable of |
| 184 | type `*bytes.Buffer`, a `typeDefinition` query will return the |
| 185 | location of the type `bytes.Buffer`. |
| 186 | Clients typically navigate to that location. |
| 187 | |
| 188 | Type constructors such as pointer, array, slice, channel, and map are |
| 189 | stripped off the selected type in the search for a named type. For |
| 190 | example, if x is of type `chan []*T`, the reported type definition |
| 191 | will be that of `T`. |
| 192 | Similarly, if the symbol's type is a function with one "interesting" |
| 193 | (named, non-`error`) result type, the function's result type is used. |
| 194 | |
| 195 | Gopls currently requires that a `typeDefinition` query be applied to a |
| 196 | symbol, not to an arbitrary expression; see https://go.dev/issue/67890 for |
| 197 | potential extensions of this functionality. |
| 198 | <!-- e.g. selecting a struct field, package name, or other piece of syntax. --> |
| 199 | |
| 200 | Client support: |
| 201 | - **VS Code**: Use [Go to Type Definition](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-implementation). |
| 202 | - **Emacs + eglot**: Use `M-x eglot-find-typeDefinition`. |
| 203 | - **Vim + coc.nvim**: ?? |
| 204 | - **CLI**: not supported |
| 205 | |
| 206 | ## Document Symbol |
| 207 | |
| 208 | The `textDocument/documentSymbol` LSP query reports the list of |
| 209 | top-level declarations in this file. Clients may use this information |
| 210 | to present an overview of the file, and an index for faster navigation. |
| 211 | |
| 212 | Gopls responds with the |
| 213 | [`DocumentSymbol`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#documentSymbol) |
| 214 | type if the client indicates |
| 215 | [`hierarchicalDocumentSymbolSupport`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#documentSymbolClientCapabilities); |
| 216 | otherwise it returns a |
| 217 | [`SymbolInformation`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#symbolInformation). |
| 218 | |
| 219 | Client support: |
| 220 | - **VS Code**: Use the [Outline view](https://code.visualstudio.com/docs/getstarted/userinterface#_outline-view) for navigation. |
| 221 | - **Emacs + eglot**: Use [`M-x imenu`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Imenu.html#Imenu) to jump to a symbol. |
| 222 | - **Vim + coc.nvim**: ?? |
| 223 | - **CLI**: `gopls links file.go` |
| 224 | |
| 225 | |
| 226 | ## Symbol |
| 227 | |
| 228 | The |
| 229 | [`workspace/symbol`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#workspace_symbol) |
| 230 | LSP query searches an index of all the symbols in the workspace. |
| 231 | |
| 232 | The default symbol matching algorithm (`fastFuzzy`), inspired by the |
| 233 | popular fuzzy matcher [FZF](https://github.com/junegunn/fzf), attempts |
| 234 | a variety of inexact matches to correct for misspellings or abbreviations in your |
| 235 | query. For example, it considers `DocSym` a match for `DocumentSymbol`. |
| 236 | |
| 237 | <!-- |
| 238 | It also supports the following special characters within queries: |
| 239 | |
| 240 | | Character | Usage | Match | |
| 241 | | --------- | --------- | ------------ | |
| 242 | | `'` | `'abc` | exact | |
| 243 | | `^` | `^printf` | exact prefix | |
| 244 | | `$` | `printf$` | exact suffix | |
| 245 | |
| 246 | However, VS Code does its own fuzzy matching afterward, so these |
| 247 | aren't effective in that client; see golang/vscode-go#647. |
| 248 | --> |
| 249 | |
| 250 | Settings: |
| 251 | - The [`symbolMatcher`](../settings.md#symbolMatcher) setting controls the algorithm used for symbol matching. |
| 252 | - The [`symbolStyle`](../settings.md#symbolStyle) setting controls how symbols are qualified in symbol responses. |
| 253 | - The [`symbolScope`](../settings.md#symbolScope) setting determines the scope of the query. |
| 254 | - The [`directoryFilters`](../settings.md#directoryFilters) setting specifies directories to be excluded from the search. |
| 255 | |
| 256 | Client support: |
| 257 | - **VS Code**: Use ⌘T to open [Go to Symbol](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-symbol) with workspace scope. (Alternatively, use Ctrl-Shift-O, and add a `@` prefix to search within the file or a `#` prefix to search throughout the workspace.) |
| 258 | - **Emacs + eglot**: Use [`M-x xref-find-apropos`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Looking-Up-Identifiers.html) to show symbols that match a search term. |
| 259 | - **Vim + coc.nvim**: ?? |
| 260 | - **CLI**: `gopls links file.go` |
| 261 | |
| 262 | |
| 263 | ## Selection Range |
| 264 | |
| 265 | The |
| 266 | [`textDocument/selectionRange`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_selectionRange) |
| 267 | LSP query returns information about the lexical extent of each piece |
| 268 | of syntax enclosing the current selection. |
| 269 | Clients may use it to provide an operation to expand the selection |
| 270 | to successively larger expressions. |
| 271 | |
| 272 | Client support: |
| 273 | - **VSCode**: Use `⌘⇧^→` to expand the selection or `⌘⇧^←` to contract it again; watch this [video](https://www.youtube.com/watch?v=dO4SGAMl7uQ). |
| 274 | - **Emacs + eglot**: Not standard. Use `M-x eglot-expand-selection` defined in [this configuration snippet](https://github.com/joaotavora/eglot/discussions/1220#discussioncomment-9321061). |
| 275 | - **Vim + coc.nvim**: ?? |
| 276 | - **CLI**: not supported |
| 277 | |
| 278 | ## Call Hierarchy |
| 279 | |
| 280 | The LSP CallHierarchy mechanism consists of three queries that |
| 281 | together enable clients to present a hierarchical view of a portion of |
| 282 | the static call graph: |
| 283 | |
| 284 | - [`textDocument/prepareCallHierarchy`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_prepareCallHierarchy) returns a list of [items](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#callHierarchyItem) for a given position, each representing a named function or method enclosing the position; |
| 285 | - [`callHierarchyItem/incomingCalls`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#callHierarchy_incomingCalls) returns the set of call sites that call the selected item; and |
| 286 | - [`callHierarchy/outgoingCalls`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#callHierarchy_incomingCalls) returns the set of functions called by the selected item. |
| 287 | |
| 288 | Invoke the command while selecting the name in a function declaration. |
| 289 | |
| 290 | Dynamic calls are not included, because it is not analytically |
| 291 | practical to detect them. So, beware that the results may not be |
| 292 | exhaustive, and perform a [References](#references) query if necessary. |
| 293 | |
| 294 | The hierarchy does not consider a nested function distinct from its |
| 295 | enclosing named function. (Without the ability to detect dynamic |
| 296 | calls, it would make little sense do so.) |
| 297 | |
| 298 | The screenshot below shows the outgoing call tree rooted at `f`. The |
| 299 | tree has been expanded to show a path from `f` to the `String` method |
| 300 | of `fmt.Stringer` through the guts of `fmt.Sprint:` |
| 301 | |
| 302 | <img title="Outgoing calls of f" src="../assets/outgoingcalls.png" width="640"> |
| 303 | |
| 304 | Client support: |
| 305 | - **VS Code**: `Show Call Hierarchy` menu item (`⌥⇧H`) opens [Call hierarchy view](https://code.visualstudio.com/docs/cpp/cpp-ide#_call-hierarchy) (note: docs refer to C++ but the idea is the same for Go). |
| 306 | - **Emacs + eglot**: Not standard; install with `(package-vc-install "https://github.com/dolmens/eglot-hierarchy")`. Use `M-x eglot-hierarchy-call-hierarchy` to show the direct incoming calls to the selected function; use a prefix argument (`C-u`) to show the direct outgoing calls. There is no way to expand the tree. |
| 307 | - **CLI**: `gopls call_hierarchy file.go:#offset` shows outgoing and incoming calls. |
| 308 | |
| 309 | |
| 310 | ## Type Hierarchy |
| 311 | |
| 312 | The LSP TypeHierarchy mechanism consists of three queries that |
| 313 | together enable clients to present a hierarchical view of a portion of |
| 314 | the subtyping relation over named types. |
| 315 | |
| 316 | - [`textDocument/prepareTypeHierarchy`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_prepareTypeHierarchy) returns an [item](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#typeHierarchyItem) describing the named type at the current position; |
| 317 | - [`typeHierarchyItem/subtypes`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#typeHierarchy_subtypes) returns the set of subtypes of the selected (interface) type; and |
| 318 | - [`typeHierarchy/supertypes`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#typeHierarchy_supertypes) returns the set of supertypes (interface types) of the selected type. |
| 319 | |
| 320 | Invoke the command while selecting the name of a type. |
| 321 | |
| 322 | As with an Implementation query, a type hierarchy query reports |
| 323 | function-local types only within the same package as the query type. |
| 324 | Also the result does not include alias types, only defined types. |
| 325 | |
| 326 | <img title="Type Hierarchy: supertypes of net.Conn" src="../assets/supertypes.png" width="400"> |
| 327 | |
| 328 | <img title="Type Hierarchy: subtypes of io.Writer" src="../assets/subtypes.png" width="400"> |
| 329 | |
| 330 | Caveats: |
| 331 | |
| 332 | - The type hierarchy supports only named types and their assignability |
| 333 | relation. By contrast, the Implementations request also reports the |
| 334 | relation between unnamed `func` types and function declarations, |
| 335 | function literals, and dynamic calls of values of those types. |
| 336 | |
| 337 | Client support: |
| 338 | - **VS Code**: `Show Type Hierarchy` menu item opens [Type hierarchy view](https://code.visualstudio.com/docs/java/java-editing#_type-hierarchy) (note: docs refer to Java but the idea is the same for Go). |
| 339 | - **Emacs + eglot**: Support added in March 2025. Use `M-x eglot-show-call-hierarchy`. |
| 340 | - **CLI**: not yet supported. |