Go logo

GoLand

A cross-platform Go IDE with extended support for JavaScript, TypeScript, and databases

Features Tutorials

Refactorings in GoLand: Rename refactoring

This is the last blog post in the series of refactorings so I wanted to save the most used refactoring the IDE has for last.

It’s one of the most powerful tools that I use and it works like magic every time. You might think that renaming things is pretty straightforward, but there are some lesser-known use cases which can really boost your productivity that I want to cover today.

But first, let’s start with this example code:

package main

import "fmt"

type User struct {
	ID       int
	Username string
	Position string
}

func main() {
	u := User{
		ID:       1,
		Username: "dlsniper",
		Position: "Developer Advocate",
	}

	fmt.Printf("User details %#v\n", u)
}

Let’s say that we want to rename the Position field to be a bit more explicit, like “JobTitle”.

We can press Shift+F6 and start renaming the field, press Enter when we are done, and that’s it.

Refactorings - Rename

However, if we change our example slightly to include a JSON string and try to call the refactoring again, then the IDE will suggest renaming the JSON field as well.

This is because the IDE understands that our string is of type string and can perform the Rename operation inside strings as well.

As with the other refactoring features, you can reject renaming the field at any time by selecting the usage you want to omit and pressing the Delete key.

If you press Shift + F6 twice, then you’ll get the following dialog which further allows you to configure where the rename operation takes place and how it should behave.

package main

import "fmt"

type User struct {
	ID       int
	Username string
	// Position field holds the job title of a user
	Position string
}

// language=json
const userJSON = `{"ID": 1, "Username":  "dlsniper", "Position": "Developer Advocate"}`

func main() {
	u := User{
		ID:       1,
		Username: "dlsniper",
		Position: "Developer Advocate",
	}

	fmt.Printf("User details %#v\nUser as JSON %s", u, userJSON)
}

Refactorings - Rename 2

Since conflicts can appear during the rename operation, the IDE will also handle this for you and allow you to correct this before the code becomes polluted by an accidental naming collision.

Refactorings - Rename 3

This is not all. The rename operation is available across a wide range of languages and support tools as well.

For example, if we connect the IDE to a database, then it can also rename database fields directly from the query that we have.
We can see this in action in the example below:

package main

import (
	"fmt"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

type User struct {
	ID       int
	Username string
	JobTitle string
}

const (
	dsn          = "postgres://postgres:postgres@10.0.75.1:5432/goland?sslmode=disable"
	getUserQuery = "select * from users where position = $1"
)

func main() {
	db, err := sqlx.Open("postgres", dsn)
	if err != nil {
		panic(err)
	}

	err = db.Ping()
	if err != nil {
		panic(err)
	}

	u := User{}
	err = db.Get(&u, getUserQuery, "Developer Advocate")
	if err != nil {
		panic(err)
	}

	fmt.Printf("User details: %#v\n", u)
}

Refactorings - Rename 4

To learn how to connect the IDE to a database, you can visit our documentation.

This concludes our short series on IDE refactoring tools. We went from changing function signatures to moving identifiers across files and even packages, to renaming them along with any other reference and even with database fields.

I hope you will find this series useful and it will increase your productivity whether you are just starting with Go, you are new to GoLand, or you are a more experienced user. In case you missed our previous posts, here’s the list of the previous topics in the series:

– Rename
Move & Copy
– Extract variable/constant
– Extract function/method
– Inline variable/constant
– Change Signature
Extract Interface (new in 2019.1)

Please let us know in the comments section below, on Twitter, or on our issue tracker your thoughts on this and what other topics we can handle in the future.

Feedback on the IDE itself is also welcome and we look forward to hearing from you on how to make your experience more enjoyable and productive.

image description