| 1 | --- |
| 2 | title: "Gopls release v0.16.0 (Jun 2024)" |
| 3 | --- |
| 4 | |
| 5 | ``` |
| 6 | go install golang.org/x/tools/[email protected] |
| 7 | ``` |
| 8 | |
| 9 | This release includes several features and bug fixes, and is the first |
| 10 | version of gopls to support Go 1.23. To install it, run: |
| 11 | |
| 12 | ## New support policy; end of support for Go 1.19 and Go 1.20 |
| 13 | |
| 14 | **TL;DR: We are narrowing gopls' support window, but this is unlikely to |
| 15 | affect you as long as you use at least Go 1.21 to build gopls. This doesn't |
| 16 | affect gopls' support for the code you are writing.** |
| 17 | |
| 18 | This is the last release of gopls that may be built with Go 1.19 or Go 1.20, |
| 19 | and also the last to support integrating with go command versions 1.19 and |
| 20 | 1.20. If built or used with either of these Go versions, it will display |
| 21 | a message advising the user to upgrade. |
| 22 | |
| 23 | When using gopls, there are three versions to be aware of: |
| 24 | |
| 25 | 1. The _gopls build go version_: the version of Go used to build gopls. |
| 26 | 2. The _go command version_: the version of the go list command executed by |
| 27 | gopls to load information about your workspace. |
| 28 | 3. The _language version_: the version in the go directive of the current |
| 29 | file's enclosing go.mod file, which determines the file's Go language |
| 30 | semantics. |
| 31 | |
| 32 | This gopls release, v0.16.0, is the final release to support Go 1.19 and Go |
| 33 | 1.20 as the _gopls build go version_ or _go command version_. There is no |
| 34 | change to gopls' support for all _language versions_--in fact this support has |
| 35 | somewhat improved with the addition of the `stdversion` analyzer (see below). |
| 36 | |
| 37 | Starting with [email protected], which will be released after Go 1.23.0 is released |
| 38 | in August, gopls will only support the latest version of Go as the |
| 39 | _gopls build go version_. |
| 40 | However, thanks to the [forward compatibility](https://go.dev/blog/toolchain) |
| 41 | added to Go 1.21, any necessary toolchain upgrade should be handled |
| 42 | automatically for users of Go 1.21 or later, just like any other dependency. |
| 43 | Additionally, we are reducing our _go command version_ support window from |
| 44 | 4 versions to 3. Note that this means if you have at least Go 1.21 installed on |
| 45 | your system, you should still be able to `go install` and use [email protected]. |
| 46 | |
| 47 | We have no plans to ever change our _language version_ support: we expect that |
| 48 | gopls will always support developing programs that target _any_ Go version. |
| 49 | |
| 50 | By focusing on building gopls with the latest Go version, we can significantly |
| 51 | reduce our maintenance burden and help improve the stability of future gopls |
| 52 | releases. See the newly updated |
| 53 | [support policy](https://github.com/golang/tools/tree/master/gopls#support-policy) |
| 54 | for details. Please comment on golang/go#65917 if |
| 55 | you have concerns about this change. |
| 56 | |
| 57 | ## Configuration changes |
| 58 | |
| 59 | - The experimental `allowImplicitNetworkAccess` setting is deprecated (but not |
| 60 | yet removed). Please comment on golang/go#66861 if you use this |
| 61 | setting and would be impacted by its removal. |
| 62 | |
| 63 | ## New features |
| 64 | |
| 65 | ### Go 1.23 support |
| 66 | |
| 67 | This version of gopls is the first to support the new language features of Go 1.23, |
| 68 | including |
| 69 | [range-over-func](https://go.dev/wiki/RangefuncExperiment) iterators |
| 70 | and support for the |
| 71 | [`godebug` directive](https://go.dev/ref/mod#go-mod-file-godebug) |
| 72 | in go.mod files. |
| 73 | |
| 74 | ### Integrated documentation viewer |
| 75 | |
| 76 | Gopls now offers a "Browse documentation" code action that opens a |
| 77 | local web page displaying the generated documentation for Go packages |
| 78 | and symbols in a form similar to https://pkg.go.dev. |
| 79 | The package or symbol is chosen based on the current selection. |
| 80 | |
| 81 | Use this feature to preview the marked-up documentation as you prepare API |
| 82 | changes, or to read the documentation for locally edited packages, |
| 83 | even ones that have not yet been saved. Reload the page after an edit |
| 84 | to see updated documentation. |
| 85 | |
| 86 | <img title="Browse documentation for package" src="../assets/code-action-doc.png" width="80%"> |
| 87 | |
| 88 | As in `pkg.go.dev`, the heading for each symbol contains a link to the |
| 89 | source code of its declaration. In `pkg.go.dev`, these links would refer |
| 90 | to a source code page on a site such as GitHub or Google Code Search. |
| 91 | However, in gopls' internal viewer, clicking on one of these links will |
| 92 | cause your editor to navigate to the declaration. |
| 93 | (This feature requires that your LSP client honors the `showDocument` downcall.) |
| 94 | |
| 95 | <img title="Symbol links navigate your editor to the declaration" src="../assets/browse-pkg-doc.png" width="80%"> |
| 96 | |
| 97 | Editor support: |
| 98 | |
| 99 | - VS Code: use the "Source action > Browse documentation for func fmt.Println" menu item. |
| 100 | Note: source links navigate the editor but don't yet raise the window yet. |
| 101 | Please upvote microsoft/vscode#208093 and microsoft/vscode#207634 (temporarily closed). |
| 102 | - Emacs: requires eglot v1.17. Use `M-x go-browse-doc` from github.com/dominikh/go-mode.el. |
| 103 | |
| 104 | The `linksInHover` setting now supports a new value, `"gopls"`, |
| 105 | that causes documentation links in the Markdown output |
| 106 | of the Hover operation to link to gopls' internal doc viewer. |
| 107 | |
| 108 | ### Browse free symbols |
| 109 | |
| 110 | Gopls offers another web-based code action, "Browse free symbols", |
| 111 | which displays the free symbols referenced by the selected code. |
| 112 | |
| 113 | A symbol is "free" if it is referenced within the selection but |
| 114 | declared outside of it. The free symbols that are variables are |
| 115 | approximately the set of parameters that would be needed if the block |
| 116 | were extracted into its own function. |
| 117 | |
| 118 | Even when you don't intend to extract a block into a new function, |
| 119 | this information can help you to tell at a glance what names a block |
| 120 | of code depends on. |
| 121 | |
| 122 | Each dotted path of identifiers (such as `file.Name.Pos`) is reported |
| 123 | as a separate item, so that you can see which parts of a complex |
| 124 | type are actually needed. |
| 125 | |
| 126 | The free symbols of the body of a function may reveal that |
| 127 | only a small part (a single field of a struct, say) of one of the |
| 128 | function's parameters is used, allowing you to simplify and generalize |
| 129 | the function by choosing a different type for that parameter. |
| 130 | |
| 131 | <img title="Browse free symbols" src="../assets/browse-free-symbols.png" width="80%"> |
| 132 | |
| 133 | Editor support: |
| 134 | |
| 135 | - VS Code: use the `Source action > Browse free symbols` menu item. |
| 136 | - Emacs: requires eglot v1.17. Use `M-x go-browse-freesymbols` from github.com/dominikh/go-mode.el. |
| 137 | |
| 138 | ### Browse assembly |
| 139 | |
| 140 | Gopls offers a third web-based code action, "Browse assembly for f", |
| 141 | which displays an assembly listing of the declaration of the function |
| 142 | f enclosing the selected code, plus any nested functions such as |
| 143 | function literals or deferred calls. |
| 144 | |
| 145 | Gopls invokes the compiler to generate the report; |
| 146 | reloading the page updates the report. |
| 147 | |
| 148 | The machine architecture is determined by the build |
| 149 | configuration that gopls selects for the current file. |
| 150 | This is usually the same as your machine's GOARCH unless you are |
| 151 | working in a file with `go:build` tags for a different architecture. |
| 152 | |
| 153 | <img title="Browse assembly for function" src="../assets/browse-assembly.png" width="80%"> |
| 154 | |
| 155 | Gopls cannot yet display assembly for generic functions: |
| 156 | generic functions are not fully compiled until they are instantiated, |
| 157 | but any function declaration enclosing the selection cannot be an |
| 158 | instantiated generic function. |
| 159 | |
| 160 | <!-- Clearly the ideal UX for generic functions is to use the function |
| 161 | symbol under the cursor, e.g. Vector[string], rather than the |
| 162 | enclosing function; but computing the name of the linker symbol |
| 163 | remains a challenge. --> |
| 164 | |
| 165 | Editor support: |
| 166 | |
| 167 | - VS Code: use the "Source action > Browse assembly for f" menu item. |
| 168 | - Emacs: requires eglot v1.17. Use `M-x go-browse-assembly` from github.com/dominikh/go-mode.el. |
| 169 | |
| 170 | ### `unusedwrite` analyzer |
| 171 | |
| 172 | The new |
| 173 | [unusedwrite](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/unusedwrite) |
| 174 | analyzer reports assignments, often to fields of structs, that have no |
| 175 | effect because, for example, the struct is never used again: |
| 176 | |
| 177 | ```go |
| 178 | func scheme(host string) string { |
| 179 | u := &url.URL{ |
| 180 | Host: host, // "unused write to field Host" (no need to construct a URL) |
| 181 | Scheme: "https:", |
| 182 | } |
| 183 | return u.Scheme |
| 184 | } |
| 185 | ``` |
| 186 | |
| 187 | This is at best an indication that the code is unnecessarily complex |
| 188 | (for instance, some dead code could be removed), but often indicates a |
| 189 | bug, as in this example: |
| 190 | |
| 191 | ```go |
| 192 | type S struct { x int } |
| 193 | |
| 194 | func (s S) set(x int) { |
| 195 | s.x = x // "unused write to field x" (s should be a *S pointer) |
| 196 | } |
| 197 | ``` |
| 198 | |
| 199 | ### `stdversion` analyzer |
| 200 | |
| 201 | The new |
| 202 | [`stdversion`](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/stdversion) |
| 203 | analyzer warns about the use of too-new standard library symbols based on the |
| 204 | version of the `go` directive in your `go.mod` file. This improves our support |
| 205 | for older _language versions_ (see above), even when gopls is built with |
| 206 | a recent Go version. |
| 207 | |
| 208 | Consider the go.mod file and Go file below. |
| 209 | The declaration of `var `alias refers to a type, `types.Alias`, |
| 210 | introduced in go1.22, but the file belongs to a module that requires |
| 211 | only go1.21, so the analyzer reports a diagnostic: |
| 212 | |
| 213 | ``` |
| 214 | module example.com |
| 215 | go 1.21 |
| 216 | ``` |
| 217 | |
| 218 | ```go |
| 219 | package p |
| 220 | |
| 221 | import "go/types" |
| 222 | |
| 223 | var alias types.Alias // types.Alias requires go1.22 or later (module is go1.21) |
| 224 | ``` |
| 225 | |
| 226 | When an individual file is build-tagged for a release of Go other than |
| 227 | than module's version, the analyzer will apply appropriate checks for |
| 228 | the file's version. |
| 229 | |
| 230 | ### Two more vet analyzers |
| 231 | |
| 232 | The [framepointer](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/framepointer) |
| 233 | and [sigchanyzer](https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/sigchanyzer) |
| 234 | analyzers have long been part of go vet's suite, |
| 235 | but had been overlooked in previous versions of gopls. |
| 236 | |
| 237 | Henceforth, gopls will always include any analyzers run by vet. |
| 238 | |
| 239 | ### Hover shows size/offset info, and struct tags |
| 240 | |
| 241 | Hovering over the identifier that declares a type or struct field now |
| 242 | displays the size information for the type: |
| 243 | |
| 244 | <img title="struct size info" src="../assets/hover-size-struct.png"> |
| 245 | |
| 246 | and the offset information for the field: |
| 247 | |
| 248 | <img title="field size/offset info" src="../assets/hover-size-field.png"> |
| 249 | |
| 250 | In addition, it reports the percentage of wasted space due to |
| 251 | suboptimal ordering of struct fields, if this figure is 20% or higher: |
| 252 | |
| 253 | <img title="a struct with wasted space" src="../assets/hover-size-wasteful.png"> |
| 254 | |
| 255 | In the struct above, alignment rules require each of the two boolean |
| 256 | fields (1 byte) to occupy a complete word (8 bytes), leading to (7 + 7) / (3 \* 8) = 58% waste. |
| 257 | Placing the two booleans together would save a word. |
| 258 | |
| 259 | This information may be helpful when making space optimizations to |
| 260 | your data structures, or when reading assembly code. |
| 261 | |
| 262 | Also, hovering over a reference to a field with a struct tag now also |
| 263 | display the tag: |
| 264 | |
| 265 | <img title="hover shows field tag" src="../assets/hover-field-tag.png"> |
| 266 | |
| 267 | ### Hover and "Go to Definition" work on symbols in doc comments |
| 268 | |
| 269 | Go 1.19 added support for [doc links](https://go.dev/doc/comment#links), |
| 270 | allowing the doc comment for one symbol to reference another. |
| 271 | |
| 272 | Gopls' Hover and Definition operations now treat these links just |
| 273 | like identifiers, so hovering over one will display information about |
| 274 | the symbol: |
| 275 | |
| 276 | <img title="hover shows field tag" src="../assets/hover-doclink.png"> |
| 277 | |
| 278 | Similarly, "Go to definition" will navigate to its declaration. |
| 279 | Thanks to @rogeryk for contributing this feature. |
| 280 | |
| 281 | ## Bugs fixed |
| 282 | |
| 283 | ## Thank you to our contributors! |
| 284 | |
| 285 | @guodongli-google for the `unusedwrite` analyzer. |
| 286 | TODO: they're a xoogler; is there a more current GH account? |
| 287 | |
| 288 | @rogeryk |