Every Go developer has a story about "Add struct tags" doing the wrong thing. You pick the code action, and it cheerfully stamps camelCase json tags on every field, even though your API uses snake_case, or you wanted db tags too. The tool wasn't being dumb. It simply had no way to ask you what you wanted.
That's the gap the Go team closed in a blog post dated October 16. Interactive refactoring in gopls lets a language server pause and ask you questions before it edits your code. It's available now in gopls v0.24.0 and the official Go extension for VS Code v0.56.0, and it's built on a capability the team is proposing for the Language Server Protocol, which means other editors and languages can get it too. I'll explain the problem, show the three refactorings that matter, and tell you what to update.
The problem: code actions couldn't take parameters
The Language Server Protocol is the open standard that decouples language features from editors. Your editor, whether VS Code, Neovim or Emacs, and a language server like gopls exchange messages continuously to provide autocomplete, hover docs, diagnostics and code actions.
Here's what the Go team says about the limitation. Almost every code transformation in LSP is like a function of no parameters: you trigger it and the server immediately computes the edits. Renaming a symbol is the lone exception, because it takes one parameter, the new name, and needed its own dedicated PrepareRename request in the protocol. When context isn't enough, the server has to pick a one-size-fits-all default or not offer the refactoring at all.
The blog is clear that the limit isn't in gopls's ability to compute edits. It's the protocol's lack of a way for the server to ask for input. Some editor extensions worked around that with custom dialog code, but that leaves behind editors like Neovim and Emacs that stick to the LSP with minimal custom client code. The Go team's proposal, microsoft/language-server-protocol#1164, creates a standard mechanism for servers to collect user input before computing edits. It was designed in collaboration with the Dart team.
Gopher artwork by Renee French, CC BY 3.0, from go.dev
Image: Renee French via Wikimedia Commons, CC BY 3.0
Add struct tags, with questions
This is the one you'll use daily. Previously, "Add struct tags" always produced json tags in camelCase. With interactive refactoring, per the blog:
- Put your cursor on a struct definition and pick the Add struct tags code action.
- An input dialog appears pre-filled with json. You can change it to any combination, such as json,yaml,db.
- A second dialog lets you pick the naming convention: camelCase, snake_case or PascalCase.
- gopls applies the tags across the type.
The post's before and after example takes a UserProfile struct with ID, FirstName, LastName and EmailAddr and produces fields tagged like json:"first_name" db:"first_name". Remove struct tags got the same treatment: it now asks which tag keys to delete, so you can strip db tags and keep json rather than wiping every tag. The struct-tag work shipped in gopls v0.23.0.
I like this for a boring reason. Struct tags are the kind of repetitive editing that people automate badly with regex or copy and paste, and a wrong tag fails silently at runtime. A tool that asks first is just a safer default.
Implement an interface, with search
The second refactoring, also from v0.23.0, targets a task most Go developers do with external tools or by copying declarations. Place your cursor on a concrete type and pick "Add methods to X to implement an interface...". A text field appears where you type the interface name, and gopls makes dynamic queries to autocomplete names of types in your workspace and the standard library, with maphash.Hasher as the blog's example. If the interface has type parameters, gopls asks for a concrete type argument for each one.
The result is complete method stubs with sensible receiver names and instantiated signatures. In the example, a MyHasher type gets Equal and Hash methods with panic("unimplemented") bodies, each with a doc comment saying it implements maphash.Hasher.
Before this, tools like impl needed you to type the receiver and qualify the interface name by hand. Letting the language server search for the type is a clear improvement, mostly because you no longer need to remember which package an interface lives in.
Move declaration, in preview
This is the big one, though it isn't shipped yet. The post says Move declaration is scheduled for an upcoming gopls release, and the working implementation is already demonstrated. You place the cursor on a top-level declaration, pick "Move declaration X", and a file picker lets you choose an existing destination .go file or enter a new path.
According to the blog, gopls moves the declaration along with its dependent declarations, such as unexported helpers or methods, leaves unrelated symbols in place, updates references across the workspace and cleans up imports in both the source and destination files. The worked example moves type Foo from package from to package to. Its method Print and the unexported constant bar, which only Foo uses, move with it, while the unrelated function Baz stays behind.
Anyone who has moved a type between packages by hand knows how tedious the import cleanup is. If the preview matches the demo, this removes one of the most irritating chores in a refactor. The tracking issue is golang/go#70583.
Other refactorings this enables
The blog lists other long-awaited items the capability enables: Change Signature (golang/go#38028), Implement Interface (golang/vscode-go#1547) and Modify Struct Tags (golang/vscode-go#2002).
Looking ahead, the Go team names three directions. Change signature refactoring needs input that can't be deduced from syntax, such as the new parameter order or a default argument for existing call sites. Interactive conflict resolution would let gopls ask for guidance on naming collisions, shadowing or ambiguous references. And interactive commands beyond code edits could cover things like configuring test or benchmark runs with profiling flags for CPU, memory and tracing.
There's an Easter egg
The blog mentions that gopls v0.24.0 offers a "Slay the dragon" code action when you put the cursor on a package-level variable named dragonSlayer. It exists as a self-contained test case for editors implementing multi-step dialogs. I'd try it for fun, and if you're an editor plugin author, it's a handy way to test your client.
What to do today
- Update gopls to v0.24.0 and the Go extension for VS Code to v0.56.0.
- Try Add struct tags on a real struct and see the two dialogs.
- Try the interface action on a type that needs a standard library interface.
- If you use Neovim or Emacs, the Go team says it explored proof-of-concept client integrations for both, but it doesn't say they're ready to install, so check your plugin's release notes before expecting dialogs.
If you don't see the prompts, make sure your editor is using the updated gopls and that your editor client supports the new capability. The post says working support is available in VS Code today and describes the Neovim and Emacs work as proof-of-concept.
Should you care?
If you write Go every day, yes, it's a quality-of-life upgrade that pays off in the first week. If you're an editor or language-server author in any language, it's more significant: a standard way for servers to ask for input is a real gap in the protocol, and the Dart team being involved suggests this isn't a Go-only idea. I'd follow the discussion in the LSP issue.
Frequently Asked Questions
What is interactive refactoring in gopls?
It lets gopls ask you for input, through dialogs in your editor, before it computes code edits. It's built on a capability proposed for the Language Server Protocol.
Which versions do I need?
gopls v0.24.0 and the official Go extension for VS Code v0.56.0. Struct tag and interface refactorings first shipped in gopls v0.23.0.
Is Move declaration available?
It's shown as a preview and scheduled for an upcoming gopls release, so it isn't generally available yet.
Does this work in Neovim and Emacs?
The Go team says it explored proof-of-concept client integrations for both, but VS Code is where working support is available today.
Originally published on Mango Developer.
Top comments (0)