The Go Playground

The Go Playground, I Want to Write Go Right Now!

 

“Ok Chris, you’ve convinced me, I want to write Go, and I want to do it now!” I don’t blame you at all, in fact after talking about it so much I want to write some code too. Usually, this is the point where you’ve got to wait around even longer while you install your programming language and all of its tools. Trust me, we’ll get there too, but luckily we don’t have to wait before we jump in and play with some code. The Go project has just what we need, the Go Playground. The Go Playground is a webpage where you can write Go code and run it right in your browser without installing anything at all!

When you visit the Go Playground they even conveniently have some code already loaded for you to play around with and run. It will look something like this:

1
2
3
4
5
6
7
8
9
package main

import (
"fmt"
)

func main() {
fmt.Println("Hello, playground")
}

Pause here, visit the Go Playground and run this code then come back here and we’ll talk about what it all means.

Ok, so hopefully it’s pretty apparent to you what this program does when you run it in the Go Playground. So instead of what it does, let’s talk about the different parts of a Go program.

Let’s start right at the top with this line.

1
package main

The first line of code in every file of Go code has to start with what’s called a package declaration. Go code is organized into packages, packages let you reuse Go code from one program in another program. We’ll talk about packages in depth later, but for now, the important thing to know is that every Go program must have a package called main. The code in the main package is what is the starting point when you run a Go program.

Next up comes the import section of our program.

1
2
3
import (
"fmt"
)

Import tells Go that you want to use code from the packages that you’re importing. These packages are code that you or someone else wrote that you want to use inside of your program. In Go you cannot import a package that you don’t use. Go back to the Go Playground and add “log” on a new line after “fmt” and then run the code. You’ll get an error imported and not used: “log”. To keep your programs as small and fast as possible, Go will not let you import packages that you don’t use.

1
func main() {

Now we’re getting into the actual program that we’re going to run. The func keyword is what tells Go that you’re going to define a new function. We’ll talk about functions later, but for now what you need to know is that a function is a way of organizing code so that you can execute it by name in other parts of your code without writing all of the code again each time you want to use it. In this instance, we’re defining a function named main. Every Go program must have a function named main defined, and you can only have one main function in your whole program. When you run a Go program, what’s being run is this main function. All of the code in between the opening { and closing } is the code of the function we’re defining.

1
fmt.Println("Hello, playground")

The fmt package contains code for formatting, including in this case the println() function which, as you’ve seen will display whatever you tell it to on a new line. Try adding a few more println() statements to the code to get the hang of it. The fmt package is part of the Go standard library, which means that you don’t have to download and install the package to be able to use it. The standard library is a collection of packages that can use without doing anything extra when you install Go.

That’s it! You’ve now written your first Go code and run it using the Go Playground, without needing to install anything. There’s still lots of nuances that we haven’t gotten into yet, even in the code of our first program. We’ll get to that soon though, for now, play around with this code more. Change it some more and run it in the Go Playground to see what happens. The next thing we’ll do is install Go on your computer as once we start working on larger projects, the Go Playground can become inconvenient, but hopefully, this has started to wet your appetite for learning Go.

Chris Sotherden

Originally a native of upstate NY, I relocated to North Carolina in 2013 where I've since helped open the new R&D headquarters for Optanix. I've been with Optanix (formerly ShoreGroup) for over ten years, where I've worked my way up from entry-level to Lead Architect. I taught myself programming in high school and am an all around technology nerd from the design of programming languages and compilers, to quantum computing, and rockets. Outside of technology I've competed in powerlifting and medium distance running, as well as smoking some mean BBQ. Oh, my partner and I also breed, show, and sell fancy goldfish!

>