Change Go comments from /* */ to //
It was quite late after I started learning Go that you should not
really use /* */ comments but almost always use //, so I
had a lot of files which use the "traditional C" style comments.
This Perl script reads in all files ending in .go in the
current directory, and converts all traditional C style comments into
the C++ style comments which are the standard in Go.
It relies on three CPAN modules, File::Slurper to read and write the files, C::Tokenize for the regular expressions for comments, and File::Versions to make a backup of the old file, which it does using an Emacs-style numbered backup. This script by default just overwrites the files. I haven't had any catastrophes using it so far, but you use this at your own risk.
#!/usr/bin/env perl # Change go comments from /* to // form. use warnings; use strict; use File::Slurper qw/read_text write_text/; 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-2026. All
rights reserved.
For comments, questions, and corrections, please email
Ben Bullock
(benkasminbullock@gmail.com).
/
Privacy /
Disclaimer