Created
April 7, 2025 13:09
-
-
Save NotYusta/fc8b700d88a86ad8e397091800425faa to your computer and use it in GitHub Desktop.
Rename Package Name Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
echo -n "Enter new Go package name: " | |
read pkg_name | |
# Get the previous package name from go.mod | |
prev_pkg_name=$(grep '^module ' go.mod | awk '{print $2}') | |
# Update go.mod | |
go mod edit -module $pkg_name | |
# Escape slashes for sed | |
escaped_pkg_name=$(echo $pkg_name | sed 's/\//\\\//g') | |
escaped_prev_pkg_name=$(echo $prev_pkg_name | sed 's/\//\\\//g') | |
# Fix imports project-wide | |
find . -name "*.go" -print0 | xargs -0 sed -i -s "s/${escaped_prev_pkg_name}/${escaped_pkg_name}/g" | |
echo "Package name updated from ${prev_pkg_name} to ${pkg_name}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment