Mar 2, 2017 - How to Install Go1.8 on Ubuntu 16.04

Tags: ,

Comments

Introduction

Go is a open source programming language created at Google in 2007. It is often referred as GoLang. The language borrows lot of concepts from Algol and C. It is compiled and statically typed with support for Garbage Collection, Memory Safety and Concurrent Programming.

In this Tutorial I will guide you through downloading and installing Go1.8, as well as building a simple Hello World application.

Prerequistes

To follow this tutorial, you will need:

  • Ubuntu 16.04 server, configured with a non-root user with sudo privileges

Step 1 - Updating Package List

    sudo apt update

Step 2 - Install wget

Next, Install wget, a non-interactive utility to download files from web

    sudo apt install wget

Step 3 - Download Go 1.8 package

Next, download the latest package of Go using the following command.

    sudo wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz

Step 4 - untar and unzip the package

Next, untar and unzip the package downloaded using the following command. Also, delete the downloaded package as it is not required anymore.

    sudo tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz

Step 5 - Add to Path

Final step in the installation process is to add the go binaries to Path. To do that, add the following ling to “.bashrc” file in your home folder.

    # This is in ~/.bashrc
    PATH=$PATH:/usr/local/go/bin

Source the bashrc again by executing the following command

    source ~/.bashrc

Step 6 - Test the installation

Now that Go is intalled and the paths are set, you can test to ensure that everything is working as expected. To check, execute the following command

    go version

The output should be similar to below ,

    go version go1.8 linux/amd64

Step 7 - Create a Hello world application

Lastly, copy the below code to “test.go” file in your home directory

    package main

    import "fmt"

    func main(){
        fmt.Println("Hello, World!")
    }

Compile and run the go program by executing the following command

    go run ~/test.go

The ouput should be as below,

    Hello, World!

Conclusion

The simple Hello World program established that you have working a Go development environment. You can read more about writing efficient Go code from the below links

References

Mar 1, 2017 - SHA-1 and Git

Tags: ,

Few days ago I read the response of Linus on SHA-1 collision announced by Google. I knew that Git uses SHA-1 hash to store the identity of the object, but I was not sure how it was formed. So, I spent few hours digging into this and below are the details for those interested.

Let’s start with an empty git repo

    mkdir /tmp/gittest
    cd /tmp/gittest
    ls
    git init
    Initialized empty Git repository in /private/tmp/gittest/.git/
    tree -a
        .
        └── .git
            ├── HEAD
            ├── config
            ├── description
            ├── hooks
            │   ├── applypatch-msg.sample
            │   ├── commit-msg.sample
            │   ├── post-update.sample
            │   ├── pre-applypatch.sample
            │   ├── pre-commit.sample
            │   ├── pre-push.sample
            │   ├── pre-rebase.sample
            │   ├── prepare-commit-msg.sample
            │   └── update.sample
            ├── info
            │   └── exclude
            ├── objects
            │   ├── info
            │   └── pack
            └── refs
                ├── heads
                └── tags

As you can see there are no objects in the repo. Let’s add some content and look at the objects

    tree -a ./.git/objects
    ├── 9d
    │   └── aeafb9864cf43055ae93beb0afd6c7d144bfa4
    ├── info
    └── pack

The folder name 9d comes from the first two digits of the SHA-1 hash, the rest 38 is used in filename of the object. To get back the content of the file we added, run the below command

    # git cat-file is to cat the file the git way and
    # -p option is to pretty-print the file	

    git cat-file -p 9daeafb9864cf43055ae93beb0afd6c7d144bfa4
    test

Now, how do we create this hash? From the “Object storage format” documentation of Git, the hash is formed from “<ascii type without space> + <space> + <ascii decimal size> + <byte\0> + <binary object data>”

    # get the object size using -s option
    SIZE=`git cat-file -s 9daeafb9864cf43055ae93beb0afd6c7d144bfa4`

    # get the object content using -o option
    CONTENT=`git cat-file -p 9daeafb9864cf43055ae93beb0afd6c7d144bfa4`

    TYPE="blob"
    echo "${TYPE} ${SIZE}\0{CONTENT}" | sha1sum
    9daeafb9864cf43055ae93beb0afd6c7d144bfa4	

You can also know the hash using “git hash-object” command as show below

    echo "test" | git hash-object --stdin
    9daeafb9864cf43055ae93beb0afd6c7d144bfa4