I’ve just started programming some DevOps tasks in Go (golang) and I like it. In this article, I’m going to show how easy it is to start programming in Go.
Golang Setup
Let’s begin with the installation. Of course, there are many Linux distros that provide Go as packages. In my case, these packages are a bit too old and I want to use the current Go 1.6 version, therefore, I’m going to do it manually here:
sudo curl -O https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz
sudo tar -xvf go1.6.linux-amd64.tar.gz
sudo mv go /usr/local
Now Go binary is in place, but let’s add it to the path and make that configuration durable in your .bashrc
echo "export PATH=\$PATH:/usr/local/go/bin" >> ~/.bashrc
#reload profile
source ~/.bashrc
At this point, Go should be on the path and you can check the installation with go version
.
Organizing Go code
Contrary to many other languages a typical Go workspace contains many source repositories containing many packages and commands. Go was designed by Google and they have monolithic Codebase, therefore it is not wondering that Go works best in this paradigm in which all Go source code and dependencies are in a single workspace.
So the official Go recommendations are:
- Go programmers typically keep all their Go code in a single workspace.
- A workspace contains many version control repositories (managed by Git, for example).
- Each repository contains one or more packages.
- Each package consists of one or more Go source files in a single directory.
Let’s create a workspace and play with it.
Workspace and $GOPATH
The GOPATH
environment variable is used to specify directories outside of GOROOT
that contain the source for Go projects and their binaries. Practically GOPATH
defines the root of your current Go workspace.
Let’s create ws_go as workspace directory and point GOPATH
variable to that.
mkdir -p $HOME/ws_go
echo "export GOPATH=\$HOME/ws_go" >> ~/.bashrc
echo "export PATH=\$PATH:\$GOPATH/bin" >> ~/.bashrc
source ~/.bashrc
We also exported $GOPATH/bin
as it very convenient way to access produces binaries.
Here to say, the top level of every Go workspace consists of three directories
- src contains Go source files,
- pkg contains package objects, and
- bin contains executable commands.
More Details to that in the official documentation: How to Write Go Code
Go Hello World
Time to write first Go program
Create file ws_go/src/test/hello/hello.go
with content:
package main
import "fmt"
func main() {
fmt.Printf("Hello, World\n")
}
Here we imported build in package fmt
and used it’s function Print
to print “Hello World” to console. Interesting here is the upper case first letter.
Names are as important in Go and even have semantic effect: the visibility of a name outside a package is determined by whether its first character is upper case.
Go Command line
Let’s go through some important commands you need from begin on.
go build
Go build compiles the packages named by the import paths1, along with their dependencies, but it does not install the results.
go build test/hello`
The binary is created in the working directory of your shell, this is not always what you want.
go install
Go install does a little more:
- The executable file to $GOPATH/bin
- Cache all non-main packages imported to $GOPATH/pkg. the cache will be used in the next compile if it not changed yet.
go install test/hello`
now as you build the test you could execute by typing the name of binary (last part of the Import path) hello
or we could directly with go run
go run
Go run compiles and executes the binary in one command, however, you need to specify the absolute path to the file (or several if more involved)
go run $GOPATH/src/test/hello/hello.go
go fmt
Very useful command
go fmt $GOPATH/src/test/hello/hello.go
It formats go source code as Go source code according to Go conventions, so every Go program is formatted the same. No questions, no discussions. So execute it every time before you commit your code.
go clean
Cleanups stuff that was created by go build
(except $GOPATH/bin)
go get
Later you’ll be needed go get
to install your dependencies inconvenient way… Now let me give you just an example
go get github.com/go-sql-driver/mysql
This will download the source code of MySQL driver and install it in the right place. You can use this in your code like:
import _ "github.com/go-sql-driver/mysql"
##Next Steps Now we can compile build and execute our program. Start playing around by reading the official documentation.
- Accomplish the Tour of Go
- Read the Effective Go
- Work you through Go by Example
- Stay tuned for my next article…
Hope you liked it. Feel free to comment.
-
An import path is a string that uniquely identifies a package. The packages from the standard library are given short import paths such as “fmt” and “net/http”. If you keep your code in a source repository somewhere, then you should use the root of that source repository as your base path. For instance, github.com/user should be your base path. ↩︎