Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Neurostep/72af6fdde35b4672c3f77d35472f28d3 to your computer and use it in GitHub Desktop.
Save Neurostep/72af6fdde35b4672c3f77d35472f28d3 to your computer and use it in GitHub Desktop.
To convert function name from snake case to camel case or pascal case in go on OS X with bash 3
#!/bin/bash
if [[ $# -eq 1 ]] ;then
case $1 in
-c) option="camel" ;;
-p) option="pascal" ;;
*) echo "This option $1 is not supported."; exit 1 ;;
esac
else
echo "Give an option -c (Camel case) or -p (Pascal case)."
exit -1
fi
for f in $(find . -name "*.go"); do
for g in $(grep -h ^func $f | sed -n 's/func \([^()]*\) *(.*/\1/p' | grep _ ) $(grep -h ^func $f | sed -n 's/func (.*) \([^()]*\) *(.*/\1/p' | grep _ ); do
snake=$g
if [[ $option = "camel" ]]; then
tocase=$(echo $g | perl -pe "s/_(.)/\u\1/g")
elif [[ $option = "pascal" ]]; then
tocase=$(echo $g | perl -pe "s/_(.)/\u\1/g; s/^(.)/\u\1/g")
fi
echo "$snake -> $tocase"
for h in $(find . -name "*.go"); do
# BSD sed
sed -i "" "s/$snake/$tocase/g" $h
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment