GoLand 2024.3 Help

Using the rename refactoring

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

  1. Click a field.

  2. Press Shift+F6 and type a different name.

  3. To confirm your change, press Enter.

    Rename a field

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.

Code snippet

  • package main import "fmt" type User struct { ID int Username string Position string } func main() { u := User { ID: 1, Username: "User", Position: "Job position", } fmt.Printf("User details %#v\n", u) }

Rename refactoring for database fields

If we establish a connection to a database, we can rename database fields directly from the query.

Renaming table names

  1. Click a table name.

  2. Press Shift+F6.

  3. In the Rename dialog and type a different name.

  4. Click Refactor.

    Rename a table

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.

Code snippet

  • 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.

  1. Click a field.

  2. Press Shift+F6.

    You can perform the rename refactoring by using the Rename dialog. Press Shift+F6 two times to call the Rename dialog.

  3. Type a different name.

  4. (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.

Code snippet

  • 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) }
Last modified: 26 May 2024