Perl and Go equivalents

This is a list of equivalents in Perl and Go, meant as a quick start for a Perl programmer learning Go.

Variables
🐫 Perl
Go
♫ Notes
my $x
var x {}interface
In Go, {}interface can contain a general quantity. However, basically Go does not have untyped variable.
my $x = \$y
x := & y
my %x;
x := map[string]string
In Go, the type of the value of the hash needs to be explicitly specified. Here := explicitly creates the variable x.
Functions
🐫 Perl
Go
♫ Notes
my (undef, $x) = f ();
_, x := f()
An underscore is used in Go to discard return values of a function, similar to assigning to undef in Perl.
defined $x{key}
_, ok = x["key"]
In Go, a second argument when retrieving from a map returns a boolean which is equivalent to defined in Perl.
×
var x bool
y := false
Perl does not have any native boolean type.
$x = 99;
if ($x) {

}
x := 99
if x > 0 {

}
Go doesn't allow anything but actual booleans in if statements. Empty strings must be tested for using len statements, as in if len(s) == 0.
use utf8;
×
All Go strings and identifiers are in UTF-8, so it is as if "use utf8;" in Perl was on by default. To get a non-utf-8 "string" in Go, one uses []byte rather than string.
$x =~ tr/a/x/;
x = strings.Map(
func(a rune) rune {
if a == 'a' { return 'x'}
}, x)
In Go, strings.Map requires a function which maps from rune to rune.
map { f ($_) } split //, $s
Arrays / slices
🐫 Perl
Go
♫ Notes
my @x = (1, 2, 3);
var x [3]int
y := (1, 2, 3)
Arrays are fixed size and are rarely used in Go. Slices (similar to array references in Perl) are much more common.
my @x = (1, 'two', $three);
x
Go slices must have a type and they can only contain one type.
my $y = scalar @x
y := len(x)
push @a, $b
a = append(a, b)
append monkeys with the "underlying array" which means that all kinds of unexpected things might happen if you are not careful.
push @a, @b
a = append(a, b...)
In Go, it's possible to append one slice to another slice, but an ellipsis is necessary.
my $x = pop @a;
x := a[len(a)-1]
a = a[0:len(a)-1]
Go requires you to chop the end of the slice off yourself.
for my $y (@x)
for _, y := range x
for my $i (0..$#x) {
    my $y = $x[$i];
for i, y := range x {
The first argument returned by range is the index into the array.
ref $x
import "reflect"
...
reflect.Type(x)
The Go method requires the package "reflect" to be imported.
caller
×
cap(x)
Go's cap, the capacity of an array, has no simple equivalent in Perl. There are modules like Devel::Peek which might allow you to find the allocated size of an array, but it won't be very much use anyway.
Input/output
🐫 Perl
Go
♫ Notes
printf "%s", $x;
fmt.Printf("%s", x)
printf $fh "%s", $x;
say $x;
Strings
🐫 Perl
Go
♫ Notes
my $x = 'abc';
my $y = "abc";
x := "abc"
x := 'a' is valid in Go, but it defines a variable which can only hold a single character, a "rune".
my $cat = 'a' . "b" . $c;
cat := "a" + "b" + c
String concatenation is via @go{+} in Go.
my $multiline = <<EOF;
Multi
Line
String
EOF
multiline := `
Multi
Line
String
`
substr $x, 10, 10
x[10:20]
In Go, use slices to get substrings. The numbers in Go are always offsets, whereas the second number in Perl is a length.
sprintf ("%d %X %s\n", $a, $b, $c);
fmt.Sprintf("%d %X %s\n", a, b, c)
use utf8;
my $key = '猿蛇月金飛行機';
my $snake = substr ($key, 1, 1);
key := "猿蛇月金飛行機"
snake := key[3:6]
Go uses byte offsets into strings, there is no attempt to fake up a "single character". Go's regexp package has two different sets of functions for @go{[]byte} and @go{string} variables.
use utf8;
my $key = '猿蛇月金飛行機';
say length $key;
key := "猿蛇月金飛行機"
fmt.Println(len([]rune(key)))
To count UTF-8 entities ("runes") in Go, cast the string to a slice of runes. There is also a package function unicode/utf8.RuneCount.
my %x;
x := make(map[string]string)
Go has associative arrays but the type needs to be specified. A general associative array in Go has the type map[string]{}interface.
$s = lc $s
$s = uc $s
$s = ucfirst $s;
The Go function splits s and "title cases" each word.
join
Go requires the strings package to be imported.
my $repeat = 'abc' x 42
repeat := strings.Repeat("abc", 42)
$v:string =~ s/\Q$old/$new/
strings.Replace(s, old, new, 1)
$v:string =~ s/\Q$old/$new/g
strings.Replace(s, old, new, -1)
strings.ReplaceAll(s, old, new)
Regexes
🐫 Perl
Go
♫ Notes
quotemeta
$z =~ s/this/that/;
r := regexp.MustCompile(`(this)`)
z = r.ReplaceAllString(z, "that")
Imports
🐫 Perl
Go
♫ Notes
use Module;
import "module"
package Module;
package module
use Exporter;
@EXPORT = qw!func_a func_b func_c!
func A() {

}
To export a function from a Go package, give its name a capital letter. To not export it, make the first letter of its name lower case.
Web encodings
🐫 Perl
Go
♫ Notes
Uri::Escape/uri_escape
HTML::Entities/encode_entities
Control
🐫 Perl
Go
♫ Notes
elsif
else if
Go does not have an elsif statement.
if ($x eq $y) {
if ($x == $y) {
if x == y {
Go does not have anything like eq and friends in Perl, strings are compared using ==. Similarly for ne and so on.
||, &&
or, and
||, &&
Go does not have anything like or and friends in Perl.
next
continue
last
break
next label
continue label
last label
break label
goto label
goto label
Other
🐫 Perl
Go
♫ Notes
perltidy
go fmt, gofmt
Go has only one formatting option.
perlcritic
go vet

Web links


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