Get Going With Go

The idea of Go, or at least a programming language that eventually became Go, came nearly 10 years ago by 3 employees of Google and entered the public the domain in 2009.

Despite being around for a fairly long time, I feel like it has become increasingly more popular in the last couple of years. Maybe this is just me not being in a loop or maybe it is just my perception.

After a fellow alumni recently wrote a blog post about his experiences trying out Go, I thought it was time to give it a go (pun unintended).

I bought Introduction to Programming in Go: A Developer Resource by Caleb Doxsey which gives a brief but useful guide to get you started. I will be giving an even briefer (but also useful) guide on getting started on a Windows 10 machine.

It is actually really easy and you can get from nothing to your first script in 5 minutes.

Installing the Language

First things first. You need to install the Go compiler to be able run your Go programs. Going onto their download page, you will see a variety of download options for various operating systems and versions.

In my case, I made use of their Windows installer version. The book I mentioned above mentioned several steps including changing PATH variables, but I didn't need any of these.

Once I had run the installer, I was able to open the Command Prompt and use the command, go and see that it was doing something (in this case, showing me the help).

I am not used to development environments being so easy to set up on a Windows machine, so this was a pleasant surprise.

IDE

After reading around, the recommended IDE to use that has support for Go is Atom. You can use anything that can edit a text file, but this one has auto completion that is compatible with Go.

Again, it has a Windows installer and so was very easy to set up.

Hello, World

We are now ready to build our first Go program. Of course, it wouldn't be a beginners guide if we were not starting with the famous "Hello, World!" program.

Start by making a new file called main.go.

Open it up and add the following:

package main
import "fmt"
func main() {
    fmt.Println("Hello, World!")
}

The first line names the "package" that this file belongs to. This is very familiar to those who use Java.

The next line imports classes and functions from the built in fmt package. Again, Java developers will feel right at home here.

Next we declare a main function that is a special function that is run when this file is run (again, Java?).

The syntax for the function is pretty standard: you have the func keyword, followed by the name of the function, followed by 0 or more parameters that can be passed into the function, surrounded by parenthesis.

Although not visible in this example, you can also specify a return type. But since we are not returning anything, there is no return type.

Inside the function, is a single line. We use the Println function from within the fmt package that we imported earlier, to print some output.

One thing you will notice is that there are no semi-colons. End of lines are denoted by a new line.

Now to run this script, simply on the Command Prompt run:

go run main.go

I got to admit, I dislike the name "Go", and prefer calling it "Golang", but its one redeeming thing is the fact that you get to say "Go run main, Go!" when you run any script. Can't help but think of the original Spider-man movie...

Anyway, I digress, when we run this:

And so you can now add Go to my LinkedIn.

Hello, Variable

Now that we have said "Hello" to the entire world, the next logical step is to say "Hello" to yourself. For this we can introduce a variable.

So I have updated the script like so:

package main
import "fmt"
func main() {
    var name string = "Antony"
    fmt.Println("Hello, " + name + "!")
}

You can see the declaration of a variable. It starts with the keyword var, followed by the name, followed by the type, in this case string. I have also assigned it in the same line with a string.

Anyone who is already familiar with basic programming, will already expect that this variable, name, is only available for use inside the main function and that is true. Having that declaration outside of the function will extend its scope to the entire script.

I have used basic string concatenation using + just like you would expect (unless you have only ever coded with PHP).

So if I run this:

You can see it works.

Summary

As I said, this is a very basic quick start guide. I could keep going here on and on and on making a more complex program, but I will leave that to you. I am looking forward to learning what can be done with this language and what cool things I can build with it.


© 2012-2017