Go, also referred to as Golang, is an open-source, lower-level, statically typed programming language created by Google.
A team of Google programmers (Robert Griesemer, Rob Pike, and Ken Thompson) developed Go in 2007. Go’s primary purpose is building fast, simple, efficient, and reliable server-side and web-based applications.
Some commonly known open source applications written with Go include Dockers, Lime, InfluxDB, Kubernetes, etcd, and Terraform. Go keeps growing and increasing in popularity as it evolves, leaving many to wonder if it is the eventual replacement of programming languages such as Python, Java, C++, and others.
“[Go] really feels like “the C for the 21st century.”
– Petr Hosek, Google Senior Software Engineer
Installing Go
Installing Go on your Linux distro is effortless.
Open a terminal window (<Ctrl><Alt>T). At the command prompt, enter:
# sudo apt-get install golang
Press Y when prompted.
Configuring Golang Environment
Before using Go, we must first configure and setup our environment.
First, we must edit our ~/.profile file to add a GOPATH variable and add our Go working subdirectory to our PATH.
Use your favorite text editor and add the following lines:
to the end of ~/.profile. When satisfied with your edits, save the file.
Next, we need to load our new PATH variables into our current shell instance.
# source ~/.profile
Although our updated PATH is loaded, we still have to create the subdirectory it refers to.
# mkdir -p golang/src/sample && cd golang/src/sample
Notice that the first command (mkdir -p golang/src/sample) creates both directories that we need for our Go environment
- The workspace directory (~./golang/src).
- The project directory (~./golang/src/sample), where ‘sample‘ is our project directory. Each Go project you create should have its subdirectory within ~./golang/src.
Of course, the second part of your command merely changes us to our project directory (~./golang/src/sample).
Create and Test Your First Go Program
Now, let’s create and test our first program. Since we’re already in our project directory, use the text editor to create our first project source file.
Add the following lines to the blank file:
package main import "fmt" func main() { fmt.Printf("Hello, FOSS Linux Readers!\n") }
Save your file as sample.go. Now let’s compile our new program.
While still in the ~./golang/src/sample subdirectory, enter
# go build
After our program compiles, let’s test it.
# ./sample
Congratulations! You’ve just built and successfully tested your first Go program. Exciting isn’t it!
The Go programming language offers Linux users a great tool to not only learn a new programming language (or programming in general) but also allows for the quick creation of simple programs. They run fast and efficiently, not only in the Linux environment but also in Mac, Windows, and Android environments, as Go is a cross-platform language.
If so inclined, please install and test out Go. ‘Go‘ ahead and create more sample programs with Go. Let us know how it turns out.
Here are a few websites to help you on your journey:
- golang.org
- gowebexamples.com
- gobyexample.com
- golangbot.com
- YouTube – Learn Go Programming – Golang Tutorial for Beginners
Good luck!