How do I refactor module name in Go?
Asked Answered
S

2

25

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?

Stereotomy answered 11/2, 2020 at 9:20 Comment(3)
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.Outsoar
Like: #44549021?Doodlebug
@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.Stereotomy
S
24

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.

Stereotomy answered 12/4, 2021 at 2:43 Comment(1)
You can also press Shift+F6 on the package name in your .go file. (I'm using GoLand 2022.1.4)Prognosticate
C
5

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

Contrive answered 5/7, 2020 at 22:30 Comment(1)
great, on MacOS it is cmd+shift+rWhitefish

© 2022 - 2024 — McMap. All rights reserved.