What Is Go?

Go or Golang is a server side technology initially developed at Google. You can use Golang to build some kickass server side applications or use it to build fast command line tools.

Golang compiles to machine code, which makes it very fast and reliable. It's no wonder that this language is quickly becoming very popular and widely used.

Let's move along to installing Golang.

Installing Go

To begin installing Go you'll need to visit http://golang.org. Click on the Download Go button:

Then, again click the Download Go button under Download the Go Distribution:

Then at https://golang.org/dl download the latest version of the installer (the latest version is usually in bold text). The .pkg file is for Mac, .tar.gz for Linux, and the .msi is for Windows.

Open the installer and let it do its thing. Click continue, next, install, etc. until you reach the end of the installer.

After the installer has completed, you may need to shut down your shell prompt, terminal, or command prompt depending on which operating system you are using.

Then open up a new terminal or command prompt, type in the word go, and press enter. You should now see a list of available commands.

And just like that you’ve installed Go on your system. Next let’s take a look at how we can use these awesome new Go commands.

Running a Go script

Create a new file anywhere on your machine and call it awesome.go and add the following content inside that file:

package main  
import "fmt" 

func main() {

    fmt.Printf("The DevDojo is Awesome!")

}

Quick Note: make sure you are using double quotes instead of single quotes or you will receive an error when trying to run the program above.

After saving that file, navigate to the location of the awesome.go file in terminal or command prompt and run it by executing the following command:

$ go run awesome.go

And you should see an output that says “The DevDojo is Awesome.”

BAM! You just wrote your first Go program. Yeah… It doesn’t do much, but it’s all good because we're just getting started.

Go Packages

Packages are source files and libraries that we can re-use in any of our Go programs. Similar to NPM for Node and Composer for PHP, Go has a package manager that you can use to include packages into your Go program.

You can take a look at the many Go packages available at the Gopm Registry.

Packages are great for writing some code and easily being able to re-use it in another program you create in the future. There are also many third-party packages available to use in your app. Next, we're going to take a look at installing and using a package in another example program.

Using Go Packages

Installing Go packages is very simple. All packages should have a url and you can get that package by running:

$ go get github.com/url_to_package

Then you can use that package in your program. As a simple example we are going to use a Go package called go-spin which is a simple library to allow us to create some cool spinners and loaders in terminal. Let's go ahead and get the package, run:

$ go get github.com/tj/go-spin

Now, if you get an error that says it cannot download because $GOPATH not set, do the following: (otherwise you can skip ahead)

You will have to specify your Go path or location where packages will be installed. You can do this on a Mac by adding the following to your ~/.bashrc or ~/.zshrc file, and then running the command once:

export GOPATH=$HOME/gocode/

In the path above, I have specified that my Go packages are going to be located in HOME/gocode/or/Users/tony/gocode(HOME will get you the path to your user's home folder)

If you are on a Windows or Linux machine, you can check out the following article on where to set your GOPATH variables.


Moving on... Let's run the package installer command again.

go get github.com/tj/go-spin  

And if you run the command and don't see any kind of output, that means it has been successfully downloaded.

Note: If you check the location of your Go Path, you'll see that the package has been downloaded in the 'pkg' and 'src' folders.


Now, let's use this package in our app. Create a new file called 'spin.go' and add the following to that file:
package main

import  "fmt"  
import "time"

import "github.com/tj/go-spin"

func main(){  
    s := spin.New()
    s.Set(spin.Default);

    for i := 0; i < 30; i++ {
        fmt.Printf("\r  \033[36mcomputing\033[m %s ", s.Next())
        time.Sleep(100 * time.Millisecond)
    }

}

Then, navigate to that file from terminal or command prompt and run the program.

go run spin.go
And you'll see the spinner output in your terminal, here is an example of how that should look:

Just like that we used a third-party package to easily add a loader.

Notice: Since we've specified our GOPATH in the steps above, you won't have to do this again, so adding and testing out other Go packages will be much easier in the future.

Conclusion

In this article we only went over writing a few scripts in Golang; however, there is so much more to learn about this amazing language and how you can use it to build some awesome apps.

Check out the links below of some cool apps written in Go.

NES Emulator Written in Go

Git Service Written in Go

Fast and Flexible Static Site Generator

And Queue cheesy Go pun...

Get ready, set, Go create some awesome programs.


Login

or Sign Up

Sign Up

or Login
×
tnylea
Cheers Friend!

I'm Tony, the creator of this site. I'm trying to increase my twitter following. Want to follow me?

Follow @tnylea If not, no worries :)