| 1 | --- |
| 2 | title: "Gopls release v0.18.0 (Feb 2025)" |
| 3 | --- |
| 4 | |
| 5 | ## Configuration Changes |
| 6 | |
| 7 | <!-- TODO(rfindley): add links to relevant settings documentation --> |
| 8 | |
| 9 | - The experimental `Structured` value for the `hoverKind` option is no longer |
| 10 | supported. |
| 11 | |
| 12 | - The `gc_details` code lens has been deleted. (It was previously disabled by |
| 13 | default.) This functionality is now available through the |
| 14 | `toggleCompilerOptDetails` code action (documented below), as code |
| 15 | actions are better supported than code lenses across a range of clients. |
| 16 | |
| 17 | VS Code's special "Go: Toggle GC details" command continues to work. |
| 18 | |
| 19 | - The experimental `semanticTokenTypes` and `semanticTokenModifiers` options |
| 20 | allow selectively disabling certain types of tokens or token modifiers in |
| 21 | `textDocument/semanticTokens` responses. |
| 22 | |
| 23 | These options supersede the `noSemanticString` and `noSemanticTokenNumber` |
| 24 | options, which are now deprecated. Users can instead set |
| 25 | `"semanticTokenTypes": {"string": false, "number": false}` to achieve the |
| 26 | same result. For now, gopls still honors `noSemanticTokenString` and |
| 27 | `noSemanticToken`, but will stop supporting them in a future release. |
| 28 | |
| 29 | - The new `workspaceFiles` option allows configuring glob patterns matching |
| 30 | files that define the logical build of the workspace. This option is only |
| 31 | needed in environments that use a custom golang.org/x/tools/go/packages |
| 32 | driver. |
| 33 | |
| 34 | ## New features |
| 35 | |
| 36 | ### "{Show,Hide} compiler optimization details" code action |
| 37 | |
| 38 | This code action, accessible through the "Source Action" menu in VS |
| 39 | Code, toggles a per-directory flag that causes Go compiler optimization |
| 40 | details to be reported as diagnostics. For example, it indicates which |
| 41 | variables escape to the heap, and which array accesses require bounds |
| 42 | checks. |
| 43 | |
| 44 | TODO: add links to the complete manual for each item. |
| 45 | |
| 46 | ### New `modernize` analyzer |
| 47 | |
| 48 | Gopls now reports when code could be simplified or clarified by |
| 49 | using more modern features of Go, and provides a quick fix to apply |
| 50 | the change. |
| 51 | |
| 52 | For example, a conditional assignment using an if/else statement may |
| 53 | be replaced by a call to the `min` or `max` built-in functions added |
| 54 | in Go 1.18. |
| 55 | |
| 56 | Use this command to apply modernization fixes en masse: |
| 57 | ``` |
| 58 | $ go run golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest -fix ./... |
| 59 | ``` |
| 60 | |
| 61 | ### New `unusedfunc` analyzer |
| 62 | |
| 63 | Gopls now reports unused functions and methods, giving you near |
| 64 | real-time feedback about dead code that may be safely deleted. |
| 65 | Because the analysis is local to each package, only unexported |
| 66 | functions and methods are candidates. |
| 67 | (For a more precise analysis that may report unused exported |
| 68 | functions too, use the `golang.org/x/tools/cmd/deadcode` command.) |
| 69 | |
| 70 | ### New `hostport` analyzer |
| 71 | |
| 72 | With the growing use of IPv6, forming a "host:port" string using |
| 73 | `fmt.Sprintf("%s:%d")` is no longer appropriate because host names may |
| 74 | contain colons. Gopls now reports places where a string constructed in |
| 75 | this fashion (or with `%s` for the port) is passed to `net.Dial` or a |
| 76 | related function, and offers a fix to use `net.JoinHostPort` |
| 77 | instead. |
| 78 | |
| 79 | ### Other analyzer changes |
| 80 | |
| 81 | - The `unusedvariable` quickfix is now on by default. |
| 82 | - The `unusedparams` analyzer no longer reports finding for generated files. |
| 83 | |
| 84 | ### New `gofix` analyzer |
| 85 | |
| 86 | Gopls now reports when a function call or a use of a constant should be inlined. |
| 87 | These diagnostics and the associated code actions are triggered by "//go:fix inline" |
| 88 | directives at the function and constant definitions. |
| 89 | (See [the go:fix proposal](https://go.dev/issue/32816).) |
| 90 | |
| 91 | For example, consider a package `intmath` with a function `Square(int) int`. |
| 92 | Later the more general `Pow(int, int) int` is introduced, and `Square` is deprecated |
| 93 | in favor of calling `Pow` with a second argument of 2. The author of `intmath` |
| 94 | can write this: |
| 95 | ``` |
| 96 | //go:fix inline |
| 97 | func Square(x int) int { return Pow(x, 2) } |
| 98 | ``` |
| 99 | If gopls sees a call to `intmath.Square` in your code, it will suggest inlining |
| 100 | it, and will offer a code action to do so. |
| 101 | |
| 102 | The same feature works for constants. |
| 103 | With a constant definition like this: |
| 104 | ``` |
| 105 | //go:fix inline |
| 106 | const Ptr = Pointer |
| 107 | ``` |
| 108 | gopls will suggest replacing `Ptr` in your code with `Pointer`. |
| 109 | |
| 110 | Use this command to apply such fixes en masse: |
| 111 | |
| 112 | ``` |
| 113 | $ go run golang.org/x/tools/go/analysis/passes/inline/cmd/inline@latest -fix ./... |
| 114 | ``` |
| 115 | |
| 116 | ### "Implementations" supports generics |
| 117 | |
| 118 | At long last, the "Go to Implementations" feature now fully supports |
| 119 | generic types and functions (#59224). |
| 120 | |
| 121 | For example, invoking the feature on the interface method `Stack.Push` |
| 122 | below will report the concrete method `C[T].Push`, and vice versa. |
| 123 | |
| 124 | ```go |
| 125 | package p |
| 126 | |
| 127 | type Stack[T any] interface { |
| 128 | Push(T) error |
| 129 | Pop() (T, bool) |
| 130 | } |
| 131 | |
| 132 | type C[T any] struct{} |
| 133 | |
| 134 | func (C[T]) Push(t T) error { ... } |
| 135 | func (C[T]) Pop() (T, bool) { ... } |
| 136 | |
| 137 | var _ Stack[int] = C[int]{} |
| 138 | ``` |
| 139 | |
| 140 | ### Extract all occurrences of the same expression under selection |
| 141 | |
| 142 | When you have multiple instances of the same expression in a function, |
| 143 | you can use this code action to extract it into a variable. |
| 144 | All occurrences of the expression will be replaced with a reference to the new variable. |
| 145 | |
| 146 | ### Improvements to "Definition" |
| 147 | |
| 148 | The Definition query now supports additional locations: |
| 149 | |
| 150 | - When invoked on a return statement, it reports the location |
| 151 | of the function's result variables. |
| 152 | - When invoked on a break, goto, or continue statement, it reports |
| 153 | the location of the label, the closing brace of the relevant |
| 154 | block statement, or the start of the relevant loop, respectively. |
| 155 | |
| 156 | ### Improvements to "Hover" |
| 157 | |
| 158 | When invoked on a return statement, hover reports the types of |
| 159 | the function's result variables. |
| 160 | |
| 161 | ### UX improvements to format strings |
| 162 | |
| 163 | #### "DocumentHighlight" |
| 164 | |
| 165 | When your cursor is inside a printf-like function, gopls now highlights the relationship between |
| 166 | formatting verbs and arguments as visual cues to differentiate how operands are used in the format string. |
| 167 | |
| 168 | ```go |
| 169 | fmt.Printf("Hello %s, you scored %d", name, score) |
| 170 | ``` |
| 171 | |
| 172 | If the cursor is either on `%s` or `name`, gopls will highlight `%s` as a write operation, |
| 173 | and `name` as a read operation. |
| 174 | |
| 175 | #### "SemanticHighlight" |
| 176 | |
| 177 | Similar to the improvements to DocumentHighlight, gopls also reports formatting verbs |
| 178 | as "format" modifier for token type "string" to better distinguish them with other parts of the format string. |
| 179 | |
| 180 | ```go |
| 181 | fmt.Printf("Hello %s, you scored %d", name, score) |
| 182 | ``` |
| 183 | |
| 184 | `%s` and `%d` will have token type "string" and modifier "format". |