Skip to content

Instantly share code, notes, and snippets.

@iamucil
Last active November 2, 2024 17:57
Show Gist options
  • Save iamucil/7578dc7df7d72e1d78c8f5543db3fbcc to your computer and use it in GitHub Desktop.
Save iamucil/7578dc7df7d72e1d78c8f5543db3fbcc to your computer and use it in GitHub Desktop.
go mod edit -module {NEW_MODULE_NAME}
-- rename all imported module
find . -type f -name '*.go' \
-exec sed -i -e 's,{OLD_MODULE},{NEW_MODULE},g' {} \;
@stephenafamo
Copy link

For OSX users

find . -type f -name '*.go' \
  -exec sed -i '' -e 's/{OLD_MODULE}/{NEW_MODULE}/g' {} \;

@sinux-l5d
Copy link

For (unfortunate) Windows/Powershell users:

# Replace these values with appropriate ones
$NEW_MODULE_NAME = "NEW"
$OLD_MODULE = "OLD"

go mod edit -module $NEW_MODULE_NAME

# Rename all imported modules in .go files
Get-ChildItem -Path . -Filter '*.go' -Recurse | ForEach-Object {
    $content = Get-Content -Path $_.FullName
    $updatedContent = $content -replace $OLD_MODULE, $NEW_MODULE_NAME
    $updatedContent | Set-Content -Path $filePath
}

@dexterp
Copy link

dexterp commented May 5, 2024

For MACOS, Linux

#!/usr/bin/env sh

export CUR="github.com/..."
export NEW="github.com/..."
go mod edit -module ${NEW}
find . -type f -name '*.go' -exec perl -pi -e 's/$ENV{CUR}/$ENV{NEW}/g' {} \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment