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.

An Introduction to Mocking in Python

Recently I came across a nice article written by Naftuli, a freelancer at totpal describing the need of writing mocks to test your services.  The blog clearly explains how to develop mocks and stubs using mock library introduced in Python 3.3 (http://www.python.org/dev/peps/pep-0417/). Advantages of using this approach for testing are enormous.

You can read more about it on toptal’s blog. Refer this link: https://www.toptal.com/python/an-introduction-to-mocking-in-python

RoR: App Structure and Running the App

Pretty simple and straight forward to create a new app

chetans-mbp-2:~ cgiridhar$ rails new blog

      create 

      create  README.rdoc

      create  Rakefile

      create  config.ru

      create  .gitignore

      create  Gemfile

      create  app     

      create  app/assets/javascripts/application.js

      create  app/assets/stylesheets/application.css

      create  app/controllers/application_controller.rb

      create  app/helpers/application_helper.rb

      create  app/views/layouts/application.html.erb

      create  app/assets/images/.keep

      create  app/mailers/.keep

      create  app/models/.keep

      create  app/controllers/concerns/.keep

      create  app/models/concerns/.keep

      create  bin

      create  bin/bundle

      create  bin/rails

      create  bin/rake

      create  bin/setup

      create  config     

      create  config/routes.rb

      create  config/application.rb

      create  config/environment.rb

      create  config/secrets.yml

      create  config/environments

      create  config/environments/development.rb

      create  config/environments/production.rb

      create  config/environments/test.rb

      create  config/initializers

      create  config/initializers/assets.rb

      create  config/initializers/backtrace_silencers.rb

      create  config/initializers/cookies_serializer.rb

      create  config/initializers/filter_parameter_logging.rb

      create  config/initializers/inflections.rb

      create  config/initializers/mime_types.rb

      create  config/initializers/session_store.rb

      create  config/initializers/wrap_parameters.rb

      create  config/locales

      create  config/locales/en.yml

      create  config/boot.rb

      create  config/database.yml

      create  db      

      create  db/seeds.rb

      create  lib

      create  lib/tasks

      create  lib/tasks/.keep

      create  lib/assets

      create  lib/assets/.keep

      create  log     

      create  log/.keep

      create  public     

      create  public/404.html

      create  public/422.html

      create  public/500.html

      create  public/favicon.ico

      create  public/robots.txt

      create  test/fixtures     

      create  test/fixtures/.keep

      create  test/controllers

      create  test/controllers/.keep

      create  test/mailers

      create  test/mailers/.keep

      create  test/models

      create  test/models/.keep

      create  test/helpers

      create  test/helpers/.keep

      create  test/integration

      create  test/integration/.keep

      create  test/test_helper.rb

      create  tmp/cache

      create  tmp/cache/assets

      create  vendor/assets/javascripts

      create  vendor/assets/javascripts/.keep

      create  vendor/assets/stylesheets

      create  vendor/assets/stylesheets/.keep

         run  bundle install     

.

.

.

Bundle complete! 12 Gemfile dependencies, 54 gems now installed.

chetans-mbp-2:blog cgiridhar$ rails server

Warning: You’re using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine –all` for better startup performance.

=> Booting WEBrick

=> Rails 4.2.0 application starting in development on http://localhost:3000

=> Run `rails server -h` for more startup options

=> Ctrl-C to shutdown server

[2015-04-25 17:16:52] INFO  WEBrick 1.3.1

[2015-04-25 17:16:52] INFO  ruby 2.0.0 (2014-05-08) [universal.x86_64-darwin13]

[2015-04-25 17:16:52] WARN  TCPServer Error: Can’t assign requested address – bind(2)

[2015-04-25 17:16:52] INFO  WEBrick::HTTPServer#start: pid=87317 port=3000

RoR: Installation

Component Required to be Installed

  1. Ruby
  2. Rails Gem
  3. Database Server (Default installed SQLite)

Installation Instructions on Mac

Make sure you have appropriate Xcode version  and Xcode command Line Tools  and brew  installed

Chetans-MacBook-Pro-2:~ cgiridhar$ brew install ruby

==> Installing dependencies for ruby: readline, libyaml

==> Installing ruby dependency: readline

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.3.6.mavericks.bottle.tar.gz

######################################################################## 100.0%

==> Pouring readline-6.3.6.mavericks.bottle.tar.gz

==> Caveats

This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X provides similar software, and installing this software in

parallel can cause all kinds of trouble.

OS X provides the BSD libedit library, which shadows libreadline.

er to prevent conflicts when programs look for libreadline we are

ting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you. If you build your

own software and it requires this formula, you’ll need to add to your

build variables:

    LDFLAGS:  -L/usr/local/opt/readline/lib

    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary

🍺  /usr/local/Cellar/readline/6.3.6: 40 files, 2.1M

==> Installing ruby dependency: libyaml

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libyaml-0.1.6.mavericks.bottle.tar.gz

######################################################################## 100.0%

==> Pouring libyaml-0.1.6.mavericks.bottle.tar.gz

🍺  /usr/local/Cellar/libyaml/0.1.6: 7 files, 348K

==> Installing ruby

==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/ruby-2.1.2_3.mavericks.bottle.2.tar.gz

######################################################################## 100.0%

==> Pouring ruby-2.1.2_3.mavericks.bottle.2.tar.gz

🍺  /usr/local/Cellar/ruby/2.1.2_3: 942 files, 20M

Chetans-MacBook-Pro-2:~ cgiridhar$ brew install rbenv ruby-build

==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz

######################################################################## 100.0%

==> Caveats

To use Homebrew’s directories rather than ~/.rbenv add to your profile:

  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion add to your profile:

  if which rbenv > /dev/null; then eval “$(rbenv init -)”; fi

==> Summary

🍺  /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 7 seconds

==> Installing ruby-build dependency: openssl

==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2a-1.mavericks.bottle.tar.gz

######################################################################## 100.0%

==> Pouring openssl-1.0.2a-1.mavericks.bottle.tar.gz

==> Caveats

A CA file has been bootstrapped using certificates from the system

keychain. To add additional certificates, place .pem files in

  /usr/local/etc/openssl/certs

and run

  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in

parallel can cause all kinds of trouble.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your

own software and it requires this formula, you’ll need to add to your

build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib

    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary

🍺  /usr/local/Cellar/openssl/1.0.2a-1: 463 files, 18M

==> Installing ruby-build

==> Downloading https://github.com/sstephenson/ruby-build/archive/v20150413.tar.gz

######################################################################## 100.0%

==> ./install.sh

🍺  /usr/local/Cellar/ruby-build/20150413: 157 files, 668K, built in 6 seconds

chetans-mbp-2:~ cgiridhar$ sudo gem install rails -v 4.2.0

Building native extensions.  This could take a while…

Successfully installed nokogiri-1.6.6.2

Fetching: loofah-2.0.1.gem (100%)

Successfully installed loofah-2.0.1

Fetching: rails-html-sanitizer-1.0.2.gem (100%)

Successfully installed rails-html-sanitizer-1.0.2

Fetching: rails-deprecated_sanitizer-1.0.3.gem (100%)

Successfully installed rails-deprecated_sanitizer-1.0.3

Fetching: rails-dom-testing-1.0.6.gem (100%)

Successfully installed rails-dom-testing-1.0.6

Fetching: erubis-2.7.0.gem (100%)

Successfully installed erubis-2.7.0

Fetching: actionview-4.2.0.gem (100%)

Successfully installed actionview-4.2.0

Fetching: actionpack-4.2.0.gem (100%)

Successfully installed actionpack-4.2.0

Fetching: activemodel-4.2.0.gem (100%)

Successfully installed activemodel-4.2.0

Fetching: arel-6.0.0.gem (100%)

Successfully installed arel-6.0.0

Fetching: activerecord-4.2.0.gem (100%)

Successfully installed activerecord-4.2.0

Fetching: globalid-0.3.5.gem (100%)

Successfully installed globalid-0.3.5

Fetching: activejob-4.2.0.gem (100%)

Successfully installed activejob-4.2.0

Fetching: mime-types-2.5.gem (100%)

Successfully installed mime-types-2.5

Fetching: mail-2.6.3.gem (100%)

Successfully installed mail-2.6.3

Fetching: actionmailer-4.2.0.gem (100%)

Successfully installed actionmailer-4.2.0

Fetching: thor-0.19.1.gem (100%)Fetching: thor-0.19.1.gem

Successfully installed thor-0.19.1

Fetching: railties-4.2.0.gem (100%)

railties’s executable “rails” conflicts with /usr/bin/rails

Overwrite the executable? [yN]  y

Successfully installed railties-4.2.0

Fetching: bundler-1.9.4.gem (100%)

Successfully installed bundler-1.9.4

Fetching: sprockets-3.0.2.gem (100%)

Successfully installed sprockets-3.0.2

Fetching: sprockets-rails-2.2.4.gem (100%)

Successfully installed sprockets-rails-2.2.4

Fetching: rails-4.2.0.gem (100%)

Successfully installed rails-4.2.0

Parsing documentation for nokogiri-1.6.6.2

unable to convert “\xCA” from ASCII-8BIT to UTF-8 for lib/nokogiri/nokogiri.bundle, skipping

Installing ri documentation for nokogiri-1.6.6.2

Parsing documentation for loofah-2.0.1

Installing ri documentation for loofah-2.0.1

Parsing documentation for rails-html-sanitizer-1.0.2

Installing ri documentation for rails-html-sanitizer-1.0.2

Parsing documentation for rails-deprecated_sanitizer-1.0.3

Installing ri documentation for rails-deprecated_sanitizer-1.0.3

Parsing documentation for rails-dom-testing-1.0.6

Installing ri documentation for rails-dom-testing-1.0.6

Parsing documentation for erubis-2.7.0

Installing ri documentation for erubis-2.7.0

Parsing documentation for actionview-4.2.0

Installing ri documentation for actionview-4.2.0

Parsing documentation for actionpack-4.2.0

Installing ri documentation for actionpack-4.2.0

Parsing documentation for activemodel-4.2.0

Installing ri documentation for activemodel-4.2.0

Parsing documentation for arel-6.0.0

Installing ri documentation for arel-6.0.0

Parsing documentation for activerecord-4.2.0

Installing ri documentation for activerecord-4.2.0

Parsing documentation for globalid-0.3.5

Installing ri documentation for globalid-0.3.5

Parsing documentation for activejob-4.2.0

Installing ri documentation for activejob-4.2.0

Parsing documentation for mime-types-2.5

Installing ri documentation for mime-types-2.5

Parsing documentation for mail-2.6.3

Installing ri documentation for mail-2.6.3

Parsing documentation for actionmailer-4.2.0

Installing ri documentation for actionmailer-4.2.0

Parsing documentation for thor-0.19.1

Installing ri documentation for thor-0.19.1

Parsing documentation for railties-4.2.0

Installing ri documentation for railties-4.2.0

Parsing documentation for bundler-1.9.4

Installing ri documentation for bundler-1.9.4

Parsing documentation for sprockets-3.0.2

Installing ri documentation for sprockets-3.0.2

Parsing documentation for sprockets-rails-2.2.4

Installing ri documentation for sprockets-rails-2.2.4

Parsing documentation for rails-4.2.0

Installing ri documentation for rails-4.2.0

22 gems installed