Skip to content

Instantly share code, notes, and snippets.

@mattfenwick
Created May 22, 2018 15:56
Show Gist options
  • Save mattfenwick/5495b38d9f73d9158c9e33214764a95d to your computer and use it in GitHub Desktop.
Save mattfenwick/5495b38d9f73d9158c9e33214764a95d to your computer and use it in GitHub Desktop.
Add comments to exported functions, methods, types in a golang source tree
my $path = $ARGV[0];
# print $path;
open(my $file, "<", $path) or die "Unable to open file, $!";
my $contents = do { local $/; <$file> };
close $file;
# print $contents;
# functions
$contents =~ s/(\nfunc\s+)([A-Z][a-zA-Z0-9_]*)/\n\/\/ $2 .....$1$2/sg;
# methods
$contents =~ s/(\nfunc\s+\([^\)]*\)\s+)([A-Z][a-zA-Z0-9_]*)/\n\/\/ $2 .....$1$2/sg;
# types
$contents =~ s/(\ntype\s+)([A-Z][a-zA-Z0-9_]*)/\n\/\/ $2 .....$1$2/sg;
# print $contents;
# print "\n";
open(my $outfile, ">", $path) or die "Unable to open outfile, $!";
print $outfile $contents;
find ./pkg -type f -name "*.go" -exec perl addcomments.pl {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment