Go (Golang) is an open source programming language developed at Google. It’s a compiled and statically typed language like C/C++/Java. It’s lightweight and fast, with small memory footprint and its support for concurrency, networking and multi-processing makes it an interesting choice for specific projects.
I use Go for building small command line apps and rewriting some parts of Sinatra/Rails(Ruby based web frameworks) applications in Go. It helps me save lots of CPU power and RAM after I rewrote some CPU intensive modules in Go. Ruby(Sinatra/Rails) is my default choice for new web projects, but I’m thinking of trying Golang(net/http) for my next project, especially if the performance/speed is critical.
Setting up Go in Ubuntu [14.04]
1. Download the package from the official page.
2. Extract it (to /usr/local
or anywhere else)
sudo tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz
3. Set the path correctly
Open bash config file using nano (or your preferred text editor).
nano ~/.bashrc
And append the following line.
export GOPATH=$HOME/go export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:$GOPATH/bin export GOBIN=$GOPATH/bin
4. Reload Bash (to apply the new config)
source ~/.bashrc
Type go
in the terminal to check if the command is available. Next, you can start writing programs in Golang.
Writing your first program in Go
Lets write a simple “Hello World” Program (hello.go
) that would simply print something using fmt.
package main import "fmt" func main() { fmt.Println("Hello World!") }
Compile
go build hello.go
Run
./hello
Recommended Links
- https://tour.golang.org/ – Getting Started guide for beginners
- http://play.golang.org/ – Online compiler, Try Golang without any setup