Hello and welcome to a Go Language programming tutorial. In this series, we're going to cover setting up and the basics of using Go in a practical way. Go is a programming language where you could easily run through the basics, and then be totally lost when it comes to actually applying it to some actual task. Because of this, we're going to learn and immediately apply at the same time as we move through these concepts.
First off, why might we be interested in the Go programming language? For me, since I mainly program in Python, I am going to compare it to a high-level language like that. Go was developed by Google to solve challenges that they were facing, purpose built from the ground up. A language like Python is great for rapid development and many tasks, but, due to the GIL and dynamic typing, it can sometimes be slow. If you scale out Python to millions of instances, you pay quite the price in performance and cost as opposed to if you had written the same program in C++. The problem is, however, C++ isn't the most enjoyable language to program in, nor is it the most friendly to learn.
Go is a static-typed language (as opposed to dynamic typing with something like Python), and has concepts like concurrency baked in right from the start. For me personally, my main interest in Go is for web development, but I can see it being useful for a variety of high-volume types of tasks and just systems in general. Not only does Go have web frameworks, the language itself comes out of the gate with full web support, a built in web server, and could be considered already a low-level web framework.
To begin, we need Go. You can download Go here. In Windows, you can just grab a simple installer, a .pkg for Mac, or a tarball for Linux. If you are on Linux, make sure you add Go to your path. You can do that by:
nano ~/.profile
And then add the following to the end of the file:
export PATH=$PATH:/usr/local/go/bin
Save, exit and then do:
$ source ~/.profile
A simple Go program will just consist of a single .go
file. You can make and edit this file with whatever you want. I will personally be using Sublime Text. On Windows, it's a simple .exe
installer, on Linux you can get Sublime with:
$ sudo add-apt-repository ppa:webupd8team/sublime-text-3 $ sudo apt-get update $ sudo apt-get install sublime-text-installer
To run your files, you use your terminal / cmd.exe and use the syntax: go run YOURFILE.go
Let's write a quick basic Go program to see if we've got everything setup and ready to go:
gointro.go
package main import "fmt" func main() { fmt.Println("Welcome to Go!") }
To run this, we can do go run gointro.go
, and you should see:
Welcome to Go!
If you made it this far, congratulations on your first Go program! Let's cover briefly what each line is doing. I will do this by commenting above or beside the lines what they do. If you do not know what comments are in programming, they are ways to write notes and such in your code that will not be run as actual code when your program runs. In Go, the way you can do single-line comments is with //
and you can do multi-line comments with /* and end with */
package main //go programs consist of packages, and start with main // import a library/package. In this case we import fmt, which brings in // standard formatted I/O strings from C import "fmt" // syntax for a function in Go. // define, name, parms, and your code goes in curly braces. func main() { //print line just outputs what you mass. // Println is a function from the fmt library // we can reference other packages from fmt with . fmt.Println("Welcome to Go!") } //we'll talk more on some other specifics in the next tutorial, such as how or why this main function even runs.