GoLand 2021.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 in-line 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.

Rename the Position field

  1. Click the Position field.

  2. Press Shift+F6 and start typing JobTitle.

  3. Press Enter.

Rename a field

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.

    Rename the worker table

    1. Click the worker table.

    2. Press Shift+F6.

    3. In the Rename dialog, type actor.

    4. Click Refactor.

    Rename a table

    Code snippet

    package main import ( "fmt" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "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, "")) }

      Rename refactoring for JSON

      GoLand detects JSON fields in strings and renames all occurrences of the field throughout the code.

      Rename the name field

      1. Click the name table.

      2. Press Shift+F6. To invoke the Rename dialog, press Shift+F6 two times.

      3. Type firstName.

      4. In the Refactoring Preview tool window, preview the changes and click Do Refactor.

      Rename a JSON field

      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: 08 March 2021