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 :
http://golang.org/dl/

Download the file from this url : https://storage.googleapis.com/golang/go1.3.src.tar.gz
Run command :
wget https://storage.googleapis.com/golang/go1.3.src.tar.gz

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
./all.bash

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 : website.com by submitting password and username on the page with url :- http://website.com/login
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 :- 
http://website.com/upser_profile_page . 
4) Now get html of this whole page and print it in log as a string .

*/


package main

import (
    "code.google.com/p/go.net/publicsuffix"
    "io/ioutil"
    "log"
    "net/http"
    "net/http/cookiejar"
    "net/url"
)

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


    resp, err = client.PostForm("http://website.com/upser_profile_page", url.Values{
        "userid": {"2"},        
        })
    if err != nil {
        log.Fatal(err)
    }

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

    // print whole html of user profile data 
    log.Println(string(data))   
    
}