Friday, October 3, 2014

How to setup golang ( go) version 1.3 or higher on ubuntu

So the golang version 1.3 is out and now in stable mode . So you must be wondering how to setup golang to go go for fantastic code .
Below i have mentioned the steps and commands which you guys can copy paste to setup "GO 1.3" on your ubuntu machine .

1) Run these commands :
sudo apt-get install build-essential
sudo apt-get install mercurial git

2) we are using go-lang’s( or go) 1.3.0 version here, currently this url will provide the golang 1.3.0 (stable) . You can browse for all releases of golang from here :

Download the file from this url :
Run command :

Now extract its content such that home directory have a folder “go” and this folder has another folders inside it (one of them is “src”) . Now run these commands :

tar -xzf golang/go1.3.src.tar.gz
cd go/src

3) Now make a folder projects in home directory and in home directory edit file called “.bashrc”

and paste the following stuff (i have added projects folder in one of the settings here below)

and GOROOT is the home folder here , where we took the clone of go lang's source code

export GOROOT=/home/ubuntu/go export GOPATH=$HOME/projects export PATH=$PATH:$GOROOT/bin:$GOPATH/bin export PATH=$PATH:$GOPATH/bin 

4) save and reboot the system

5) now make a test folder in projects folder

cd ~/projects/
mkdir test
cd test
sudo apt-get install vim
vim test.go

paste the code given below :

package main 
import "fmt" 
func main() {     fmt.Printf("hello, world\n") 

now save it and run the command

go run test.go

if it says : hello, world

then everything is working fine .

if there is some problem like golang is not installed then check Path you set earlier in .bashrc file

Note : We have used "go" named folder in home directory , you can change location of this folder as per your wish but change the path in ".bashrc" file in step 3 as well .

Web scraping and bot creation with go (golang) using cookiejar

So you can easily write a "bot" or scraping program to get data from any website or to perform some tasks on your website like uploading of images and publishing data on your blog .

This code Do the following stuff :  
1) login to a website called : by submitting password and username on the page with url :-
2) Now after login using the cookies stored by this webiste access user profile page 
3) Now using same client which stored the required cookies make another post request to user profile page present at page :- . 
4) Now get html of this whole page and print it in log as a string .


package main

import (

func main() {
    options := cookiejar.Options{
        PublicSuffixList: publicsuffix.List,
    jar, err := cookiejar.New(&options)
    if err != nil {
    client := http.Client{Jar: jar}
    resp, err := client.PostForm("", url.Values{
        "password": {"loginpassword"},
        "username" : {"testuser"},        
    if err != nil {

    resp, err = client.PostForm("", url.Values{
        "userid": {"2"},        
    if err != nil {

    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {

    // print whole html of user profile data