Standardise Go function comments

This is an example Perl program which changes Go function comments using the "traditional C" format /* comment */ to the more standard C++-style comments, // comment, with no space between the comment and the function declaration, so that it will work correctly with go doc. It only does function comments for exported functions (those whose names begin with a capital letter).

You need to install two CPAN modules, File::Versions and C::Tokenize to use this script.

use Z;
use C::Tokenize ':all';
use File::Versions ':all';
my @files = <*.go>;
for my $file (@files) {
    my $text = read_text ($file);
    while ($text =~ m!(($trad_comment_re)\s*func ([A-Z]\S+))!g) {
        my $a = $1;
        my $c = $2;
        my $f = $3;
        $c = decomment ($c);
        $c =~ s!^\s*!// !gms;
        $text =~ s!\Q$a\E!$c\nfunc $f!g;
    make_backup ($file);
    write_text ($file, $text);


Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer