Using the rename refactoring
To rename code inline: ShiftF6.
To rename code by using the Rename dialog: press ShiftF6 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.
Click a field.
Press ShiftF6 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.
If we establish a connection to a database, we can rename database fields directly from the query.
tip
For more information about connecting the IDE to a database, refer to Connection to a database.
Click a table name.
Press ShiftF6.
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, "")) }
GoLand detects JSON fields in strings and renames all occurrences of the field throughout the code.
Click a field.
Press ShiftF6.
You can perform the rename refactoring by using the Rename dialog. Press ShiftF6 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 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!