How do I refactor module name in Go?

hylowaker picture hylowaker · Feb 11, 2020 · Viewed 7.5k times · Source

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?

Answer

Moacir Schmidt picture Moacir Schmidt · Jul 6, 2020

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