Golang Hello World!

Well, in the last blog we installed Go on our Mac and Ubuntu systems, now it’s time to run our first program – program to display hello, world and learn about using the Visual Studio Code editor.

Go Hello World

When you have go installed, running a Go script is like running any other program for an interpreted language. Like in Python, we run python app.py in Go we can run a script as go run hello.go. Let’s write a simple Go program to display hello, world on the terminal. Open an file hello.go and type in below source code.

At a high level – package keyword indicates this file can be used as a package, main() is the method that is called when the program is run.

Source code:

Screen Shot 2018-12-04 at 11.51.33 AM

 

Run the program as:

chetan:go-work chetan$ go run hello.go
hello, world

And done, we have a simple working Go program! 🙂

Go Editors

Cool, now let’s talk about the editors. There are multiple editors that support Go, here are a few of them:

  1. vim-go – plugin provides Go programming language support
  2. Atom – Go-Plus is an Atom package that provides enhanced Go support
  3. Visual Studio Code – Go extension provides support for the Go programming language

In this blog, we will setup Go on Visual Studio Code:

  1. Download and install Visual Studio Code from https://code.visualstudio.com/
  2. Click on the ‘Extensions’ icon and search Go for the extension
  3. Use the extension by Microsoft and install it (I think this will also install delve to debug the Go apps)

Screen Shot 2018-12-04 at 12.14.56 PM

That’s it! So now we have an editor setup and we can also run a simple Go program from the terminal.

In the next blog, we will look at the Go project structure and the dev tools that will be useful during development.

 

 

 

Advertisements

Installing Golang

Hi there, welcome back, hope you read the introduction blog and are ready to install Go.

Installing Go is easy, you can download the binaries for your platform from the Go’s download page. In this blog, we will go through the steps of installing Go on macOS and Ubuntu and install Go v1.11.2.

Install Go v1.11.2 on Mac 10

Step 1: Download the package file from https://golang.org/doc/install?download=go1.11.2.darwin-amd64.pkg

Step 2: Open the package file and follow the prompts and that’s it – Go will be installed

screen-shot-2018-12-02-at-11-25-01-am

Screen Shot 2018-12-02 at 11.25.35 AM

Step 3: Check that the package will be installed in /usr/local/go folder and the PATH environment variable will be updated with /usr/local/go/bin

Step 4: Open the terminal and run go version command, you should see the below output, which confirms the go installation and prints the installed version as well

chetan:~ chetan$ go version
go version go1.11 darwin/amd64

Install Go v1.11.2 on Ubuntu 16.04

Step1: Make sure you update your packages, security patches and fixes by running these commands on Ubuntu:

ubuntu@ip-172-31-18-70:~$ sudo apt-get update
ubuntu@ip-172-31-18-70:~$ sudo apt-get -y upgrade

Step 2: Begin downloading the latest Go package from the downloads page using our very own curl command:

ubuntu@ip-172-31-18-70:~$
curl -O https://storage.googleapis.com/golang/go1.11.2.linux-amd64.tar.gz

Step 3: Unzip the gzip file using the tar command, you will get the ‘go‘ folder after unzipping the contents. Now move the ‘go‘ folder to /usr/local/ (like in macOS installation)

ubuntu@ip-172-31-18-70:~$ tar -xvf go1.11.2.linux-amd64.tar.gz
ubuntu@ip-172-31-18-70:~$ sudo mv go /usr/local

Step 4: Use /usr/local/go/bin path to update your bash profile with PATH environment variable like the macOS installer did for us. You can also create a folder, say go-work under your home directory (in my case /home/ubuntu), this is where all our go related source code will go in. Don’t worry too much about GOPATH for now, we understand more about it in the next blog

ubuntu@ip-172-31-18-70:~$ echo $HOME
/home/ubuntu
ubuntu@ip-172-31-18-70:~$ mkdir go-work

ubuntu@ip-172-31-18-70:~$ vi ~/.bashrc
export GOPATH=$HOME/go-work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Step 5: Now reload your ~/.bashrc file (use appropriate profile file based on the shell you use, for instance use ~/.zshrc if you’re using zsh) and type go version on your terminal. And you’re done! 

ubuntu@ip-172-31-18-70:~$ source ~/.bashrc

ubuntu@ip-172-31-18-70:~$ go version
go version go1.11.2 linux/amd64

 

So now we have Go v1.11 installed on macOS and Ubuntu. Hurray! 🙂

But we haven’t written any code yet and what about the Editor? Let’s look at them in our next blog.

Introduction to Go programming language

Hi folks, it’s been quite a long time, I have been working on interesting scalability challenges recently and couldn’t get enough time to share my learning.

I have been dabbling with this interesting language and thought it’d be great for you to know as well, but here I am, this time with Golang or simply Go.

What’s Go? Let’s start with a quick introduction:

  • Go is an open source programming language and was developed in Google by Ken Thompson and Rob Pike
  • Go is a statically typed and a compiled language (like C/C++), but feels like a dynamically typed interpreted language (like Python)
  • Even though a simple language, Go is used to write efficient programs that scale well – kind of brings in best of both the worlds
  • As it is a compiled language – Go programs can be built on a machine like Mac or Ubuntu and can be distributed and run across other operating systems like Windows
  • Strangely, Golang is not object oriented but provides methods, interfaces and structures for grouping data and reuse. (So OOP or not? More about this later)
  • In the world of internet, where web apps want to use multiple cores to run their programs, Go’s built in concurrency model (with go routines and channels) is a big boon for developers
  • Go is 9 years strong (at the time of writing this blog) and v1.11.2  release is available for download, with proposals already for version 2.0 being actively discussed by the community

Enough of introduction, let’s take the first step and install Go in the next blog.