Go compared with php. Arrays and slices

This is part of a series of articles comparing the language of Go to PHP

Variable assignment

In PHP variable assigned is very simple :

$myString = "My String";
                

In Go this is a little different. Go is statically typed meaning those familiar with other statically typed languages would expect to see something like

String myString = "My String";
                

In Go the type can be inferred from the expression. For instance to assign a string to a variable in Go you can do any one of the following:

>var myString = "hello"
//or
myString := "Hello"
                

To change the value later Go a single = is used

myString := "myString"
myString = "foo"
fmt.Println(myString) //Prints out Foo
                

I prefer the way variable assignment is done in Go. It makes it very easy to see where the variable has been instantiated, where it's been changed and in 99% of times the type inference isn't an issue.

Also note that the use of semicolons in Golang is optional

If statements

If statements are very similar in both PHP and Golang in PHP
if($i < 3) {
  echo "$i is less than 3";
                     
}
In Go
if x > 0 {
   fmt.Println(x,"is greater than zero")
}
                

The biggest difference are the optional brackets around the if statement. Braces are also mandatory and the first brace must be on the same line as the if.

This fits with the psr-2 PHP fig standards and as such is really familiar to most PHP developers.

The biggest difference is it's common to see an initialization statement at the beginning of the if statement, so instead of seeing this:

$i = getMyVar();
if($i < 3) {
  echo "$i is less than 3";

}
                

You see

if x:=getMyVar(); x > 0 {
    fmt.Println(x,"is greater than zero")
}
                

Personally I'm not keen on this syntax as I find it slightly harder to realise at a glance what the if statement is checking, but this is purely a matter of preference.

Loops

Go takes an usual step when it comes to looping. The familiar while loop seen is most of languages based on C has been unified with the for loop meaning to do this

while($i < 3) {
$i++
}
                
In Go you write
for i < 4 {
    i++
}
                

When I'm writing PHP code I tend to use while for longer running loops, and for for loops over a finite set of data. I find it harder when writing Go code to see, at a glance see what type of loop, the code is performing. This has caused me to code in infinite loops on more than one occasion whilst experimenting with Go.

Go also uses the range clause to achieve similar functionality to PHP's foreach. Where in PHP you would do this

$myArrayOfThings = [1,2,3,4];
foreach($myArrayOfThings as $key => $value) {
  echo $key."-".$value."\n"; //0-1 1-2 2-3 3-4
}
                
In Go the range statement is used instead
myArrayOfInts := []int{1,2,3,4,5,6}
for key,value := range myArrayOfInts {
    fmt.Println(key,value)
}
                

One of the things I dislike about PHP is the ordering of the foreach statement, and as such I prefer the way Go does it. One minor annoyance with Go is that if you don't use a variable in the code it won't compile. So if you only wish to use the key in the above you can use the magic _ character instead.

myArrayOfInts := []int{1,2,3,4,5,6}
for key,_ := range myArrayOfInts {
    fmt.Println(key)
}
                

Functions

Functions in Go allow multiple return values which make moving from PHP to Go a lot easier than it might be moving from other dynamic languages to static languages.

In PHP a function can return multiple types, this is useful in a lot of ways for instance, dealing with errors. An example of this in PHP would be:

$result = updateSomething();
if(!$result) {
    return false;
}
echo "Updated id ".$id;
                    

In Go we can use the fact that a function can return multiple values to increase code clarity whilst achieving the same effect.

int, err := updateSomething()
if err == false {
  return false
}
fmt.Println("Updated id",id)