21

I have a Go module named mymodule, and I'd like to rename it into github.com/hylowaker/awesome-module

Using command go mod edit -module github.com/hylowaker/awesome-module only changes module name in go.mod file, leaving go sources unchanged. I tried Refactor feature in GoLand IDE, but GoLand does not allow renaming with slash(/) characters.

So I had to find and replace every import "mymodule/..." into import "github.com/hylowaker/awesome-module/... from my source files.

Is there a better way to refactor them?

hylowaker
  • 966
  • 2
  • 9
  • 24
  • 5
    This maybe a stupid comment, But wont a find and replace do the trick once you have edited it in go.mod ? You can easily do a find and replace across project in Sublime or VSCode or Goland. – Kartavya Ramnani Feb 11 '20 at 11:57
  • 2
    Like: https://stackoverflow.com/questions/44548521/how-to-change-go-package-alias-en-masse? – JimB Feb 11 '20 at 18:01
  • @Kartavya I agree find&replace is enough for most cases, but since it could mistakenly replace comment or string as well, I was looking for safer and efficient way. – hylowaker Feb 13 '20 at 01:58

2 Answers2

20

This feature is introduced in GoLand version 2021.1.

You can invoke the Rename refactoring by pressing Shift+F6 on the module name in the go.mod file.

hylowaker
  • 966
  • 2
  • 9
  • 24
  • 1
    You can also press `Shift+F6` on the package name in your `.go` file. (I'm using GoLand 2022.1.4) – gomisha Aug 16 '22 at 20:40
6

In GoLand just press Ctrl+Shift+R and execute "Replace in Path"

It is safe to perform that in entire project since you only need to change go.mod file and all import clauses

Moacir Schmidt
  • 797
  • 8
  • 9