Using the rename refactoring
Last modified: 21 April 2022To rename code inline: Shift+F6.
To rename code by using the Rename dialog: press Shift+F6 two times.
The Rename refactoring lets you change names of symbols, files and all the references to them throughout the code.
Renaming local variables or private methods can be done easily inline since only the limited scope is affected. Renaming types, interfaces, or public methods could potentially impact a lot of files. In this case, preview potential changes before you refactor.
Renaming fields
Click a field.
Press Shift+F6 and type a different name.
To confirm your change, press Enter.
Gif
You can copy the following code snippet to your editor and try to rename the Position
field to JobTitle
. Use the previous procedure for clues about shortcuts.
Rename refactoring for database fields
If we establish a connection to a database, we can rename database fields directly from the query.
tip
To learn how to connect the IDE to a database, see Database connection.
Renaming table names
Click a table name.
Press Shift+F6.
In the Rename dialog and type a different name.
Click Refactor.
Gif
You can copy the following code snippet to your editor and try to rename the worker
table to actor
. Use the previous procedure for clues about shortcuts.
package main import ( "fmt" "github.com/jmoiron/sqlx" "strings" ) type User struct { ID int firstName string } const ( dsn = "postgres://guest:guest@localhost:54332/guest?sslmode=disable" getUserQuery = "SELECT first_name FROM worker WHERE actor_id = 1" ) func main() { db, err := sqlx.Open("postgres", dsn) if err != nil { panic(err) } err = db.Ping() if err != nil { panic(err) } firstName := []string{} err = db.Select(&firstName, getUserQuery) if err != nil { panic(err) } fmt.Printf("First name: %#v\n", strings.Join(firstName, "")) }
Renaming JSON fields
GoLand detects JSON fields in strings and renames all occurrences of the field throughout the code.
Click a field.
Press Shift+F6.
You can perform the rename refactoring by using the Rename dialog. Press Shift+F6 two times to call the Rename dialog.
Type a different name.
(Optional) If you use In the Refactoring Preview tool window, preview the changes and click Do Refactor.
You can copy the following code snippet to your editor and try to rename the name
JSON field to firstName
. Use the previous procedure for clues about shortcuts.
package main import "fmt" type User struct { ID int name string lastName string } // language=json const userJSON = `{"ID": 1, "name": "John", "lastName": "Smith"}` func main() { u := User{ ID: 1, name: "John", lastName: "Smith", } fmt.Printf("User details %#v\nUser as JSON %s", u, userJSON) }
Thanks for your feedback!