Thoughts on the Basics of Go

Disclaimer: I’ve been studying Go for two days. There are some neat things I learned about it, so I decided to document them, but if anything in this post is factually inaccurate, please let me know so I can correct it. Thanks!

Go is an object-oriented, statically-typed programming language created by Google in 2007, which makes it the only language I know that’s younger than me. (Python and JavaScript aren’t as young as you think they are, they were both created in the mid-90s. Yeah, I was surprised too.) Go seems to me like a weird lovechild of Java and JavaScript, but then again, I have heard it’s more like C, I just didn’t make that connection because I don’t know C.

That being said, if you already know both Java (or some other statically-typed OO language like C) and JavaScript (or some other dynamically-typed language like Python) most of the basics of Go aren’t going to seem odd to you. Go is statically typed, but it’s much smarter than Java in terms of inferring variable types, so the actual top-level programming feels somewhat like JavaScript or Python.

For some examples: Go has a main method that works just like Java, and one variable initialization style, var x int = 10, looks Java-esque. However, the idiomatic style for initializing variables looks somewhat like JavaScript: x := 10, since there’s no explicit type declaration. Overall, in Go, you type way less characters than you do in any other language I’ve used before. Look at the idiomatic initialization above. Or look at how you get a character from an array: arr[2], unlike Java, arr.charAt(2). The + operator is also overloaded to both addition and concatenation, like JavaScript, unlike the Java string.concat() method.

Now for the weird stuff that’s not like any other language I know – though it might be like some other language.

Go only has one loop, which happens to use the for keyword. Using different syntax patterns, it can operate like a for loop, a for-each loop, and a while loop, just to name a few. There’s also a range loop for k := range arr, and if another language I know uses them it’s definitely esoteric.

Go also does a weird thing with arrays. There are three standard types of array-like things: arrays, slices, and maps. Arrays pretty much work like Java: they have a set type and size and can’t be expanded. To fix the “arrays can’t be expanded but I wannaaaaa” problem, instead of going the Java route and making an array-style expandable object type which everyone then uses instead of standard arrays (ArrayList), Go has “slices”. These are segments of arrays which can be expanded when new elements are appended, until the length of the slice exceeds the length of the array, at which point the contents of the array are copied to a new array which is big enough to hold the appended elements. …I think. Maps are, fortunately, simpler. They’re sets of key-value pairs, where the keys and values can be of any single type. So you can map strings to ints, ints to ints, strings to strings, etc.

There are two things about Go which I find neither good nor bad, just very interesting. The first thing is that, among the variously-sized numeric types, there exist numeric types for complex numbers. This sparked a sudden desire in my mind to do something involving quantum physics with Go, although I have absolutely no idea what about quantum physics I would want to write a computer program for. The second thing is that Go does a lot of stuff in the Terminal, which makes me wonder how one goes about writing desktop applications in the language. (According to this article, it’s difficult.) I’m not a huge fan of web development stuff in general, and it’d be kind of annoying to have to make all my advanced Go projects web-based.

Besides slices, nothing about Go seems too awful or counterintuitive, so I’m optimistic about my future studies. As soon as I’ve got something a bit more complicated than Hello World, I’ll post projects and write-ups here. Please look forward to it!

Leave a Reply

Your email address will not be published. Required fields are marked *