Disclaimer: This article is not and does not intend to be an introduction to the Go programming language.

Recently I’ve been learning the Go language created by Googlers, it’s a nice and elegant new language that seems to have a bright future.

Coming from a PHP background, there are a few things that I totally don’t understand (initially). PHP is a loosely typed programming language, you can convert int to string anytime anywhere and convert back as well. It’s the first time I meet up with a static typing language, that caused me quite some trouble in the beginning but I have been getting used to it ever since (and maybe beginning to appreciate it as well).

Besides that I have totally no concept about pointers as well, I’ve dealt them a little in PHP but in other languages like C, C++ it’s all about pointers. That makes it difficult for me. Luckily, Go has very little to do with pointers, I was quite relieved when I knew about that.

PHP array equivalence

Well, there are arrays in Go, but they’re different from PHP. For instance, to create an array one can do something like this:

1
for ($i=1;$i<=10;$i++) $array[] = $i;

It is possible to append to an array directly with $array[] and you don’t have to initialize nor declare the length of the array before you use it. In Go it’s a totally different thing (basically, in all other languages it is a totally different thing), you need to declare and initialize the array before use.

1
2
3
4
tmpArray := [10]int
for i := 1; i <= 10; i++ {
    tmpArray[i-1] = i
}

Note that you can’t just append items to arrays with tmpArray[], you have to specify an index. But what if I don’t know exactly how big my array is gonna be? That’s when you should use slice. A slice allows you to dynamically extend your arrays, and make it as long as possible.

1
2
3
4
tmpArray := make([]int, 0)
for i := 1; i <= 10; i++ {
    tmpArray = append(tmpArray, i)
}

Note that I declared and initialized a tmpArray with 0 length, then I looped 10 times and added items to it. But I suppose that this operation is expensive because if you need to extend the slice’s size it will initialize a new slice with the larger length, copy all items over and give it back to you, so it’s always good to either determine how large you need the slice to be before you begin.

What about associative arrays? Associative arrays in PHP are extremely useful and I personally use them all the time. In Go however, they aren’t called arrays, they’re maps.

To initialize a map, you do make(map[string]string), code example as below:

1
2
maps := make(map[string]string)
maps["test"] = "Hello!"

Notice that you can assign new items right away. If you want to assign when you declare the variable, you can do so:

1
maps := map[string]string {"test": "Hello!"}

A very handy and useful functionality in Go.

Regular Expressions In Go

I can’t cover too much in this area, as I have also yet to explore on regular expressions in Go yet. But I can provide some basic information on how to use regular expressions in Go.

In PHP, we do preg_match("/([a-zA-Z0-9]+)/", $string, $results) to find all alphanumeric characters. The equivalence in Go is the following:

1
2
3
import "regexp"
rx_alphanumeric := reegxp.MustCompile("([a-zA-Z0-9]+)")
match := rx_alphanumeric.FindStringSubmatch(stringToFind)

You then have all the matches in match, which is an array of type [][]string, all the match results are stored in match[1] just like in php. Besides FindStringSubmatch, there are still a lot of functions you should read the documentation.

isset()

Performing an operation on an uninitialized variable will cause the whole program to terminate immediately. PHP has a useful function called isset(), but how do we use the equivalent on Go?

It turns out, in Go, assignments actually return 2 variables. The 2nd variable indicates if the operation is a success. So if you’re not sure whether the variable notSet is set or not, do the following:

1
2
3
4
5
if val, exists := notSet; exists {
    println("Is set!")
} else {
    println("Not set")
}

Conclusion

The only painful part of transitioning from PHP to Go is that every time you make some changes you have to recompile it to see the effect. In PHP you just have to tap Cmd+R in your browser to see the changes in effect. Besides that, resources on the Go programming language is still scarce and libraries aren’t quite available yet.

Other than that, I don’t see any reasons why we shouldn’t use Go.