Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refactor module name in Go?

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?

like image 765
hylowaker Avatar asked Feb 11 '20 09:02

hylowaker


People also ask

How do I rename a project in Golang?

Press Shift+F6 or from the main menu, select Refactor | Rename.

What is Go mod command?

Go modules commonly consist of one project or library and contain a collection of Go packages that are then released together. Go modules solve many problems with GOPATH , the original system, by allowing users to put their project code in their chosen directory and specify versions of dependencies for each module.


2 Answers

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.

like image 76
hylowaker Avatar answered Sep 18 '22 17:09

hylowaker


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

like image 31
Moacir Schmidt Avatar answered Sep 18 '22 17:09

Moacir Schmidt