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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

packagemain

import"fmt"

typeUserstruct{

ID int

Username string

Position string

}

funcmain(){

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.

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.

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: