Asynchronous http request in golang

type HttpResp struct {
	Id   string
	Resp *http.Response
	Err  error
}

func AsyncGet(urls map[string]string) []*HttpResp {
	ch := make(chan *HttpResp)
	responses := []*HttpResp{}

	for track_id, url := range urls {

		go func(i, u string) {
			resp, err := http.Get(u)
			ch <- &HttpResp{i, resp, err}
		}(track_id, url)
	}

loop:
	for {
		select {
		case r := <-ch:
			responses = append(responses, r)
			if len(responses) == len(urls) {
				break loop
			}
		case <-time.After(50 * time.Millisecond):
			fmt.Printf(".")
		}
	}
	return responses
}

Download file from internet with Golang

package main

import "os"
import "log"
import "fmt"
import "time"
import "strconv"
import "io/ioutil"
import "net/http"
import "path/filepath"
import "crypto/rand"

func main() {
	download_link := "http://golang.org/doc/gopher/frontpage.png"
	path, err := download_file(download_link)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(path)
}

func download_file(download_link string) (string, error) {
	ext := filepath.Ext(download_link)

	dir_path, err := dir_full_path()
	if err != nil {
		return "", err
	}
	file_name := rand_str(10) + ext
	file_path := dir_path + file_name

	os.Mkdir(dir_path, 0666)

	file, err := os.Create(file_path)
	if err != nil {
		return "", err
	}
	defer file.Close()

	res, err := http.Get(download_link)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	file_content, err := ioutil.ReadAll(res.Body)

	if err != nil {
		return "", err
	}

	// returns file size and err
	_, err = file.Write(file_content)

	if err != nil {
		return "", err
	}

	return file_path, nil
}

func dir_full_path() (string, error) {
	path, err := filepath.Abs("files")

	if err != nil {
		return "", err
	}

	t := time.Now()

	s := path +
		string(os.PathSeparator) +
		strconv.Itoa(t.Day()) +
		"_" +
		strconv.Itoa(int(t.Month())) +
		"_" +
		strconv.Itoa(t.Year()) +
		string(os.PathSeparator)

	return s, nil
}

func rand_str(n int) string {
	alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

	var bytes = make([]byte, n)
	rand.Read(bytes)
	for i, b := range bytes {
		bytes[i] = alphanum[b%byte(len(alphanum))]
	}
	return string(bytes)
}

create a list of struct in golang

type Track_list struct {
Song_name string
Song_hash string
Artist_name string
Artist_hash string
}

songs_list := []Track_list{}

for _, item := range content.Toptracks.Track {
songs_list = append(songs_list, Track_list{item.Name, item.Mbid, item.Artist.Name, item.Artist.Mbid})
}

golang get request


url := "http://example.com/"

// make a get request
res, err := http.Get(url)
if err != nil {
	panic(err.Error())
}
// func ReadAll return bytes
body, err := ioutil.ReadAll(res.Body)
if err != nil{
	panic(err.Error())
}
// convert byte to string and print
fmt.Println(string(body))
os.Exit(0)

Drag drop in javaScript (coffeescript)


# helper function
put = ()-> console.log "Arguments : ", arguments

###
get position when it was clicked.
###

class Drag_me
	constructor: (@bg_div)->

	make_dynamic:()->
		bg_el = $ @bg_div

		mouse_down = false

		bg_el.mousedown (e)->
			# mouse_down
			mouse_down = true

			# see if it was right click, (1 = right click, 2 = middle click, 3 for left click)
			if e.which == 1

				# some time chrome uses default cursor so to prevent this
				e.originalEvent.preventDefault();

				# change mouse icon
				bg_el.css('cursor','all-scroll')

				# get current position x stands for left and right, y stands for top to button (z for depth)
				origin_x = e.clientX
				origin_y = e.clientY

				# get current position
				left = parseInt(bg_el.css('left'), 10)
				top = parseInt(bg_el.css('top'),  10)

				$(this).mousemove (e)->

					# if mouse click is not active then don't do anything.
					if mouse_down is true

						# get distance then minus with original mouse position (when it was clicked),
						# now we have the mouse distance
						x = e.clientX - origin_x
						y = e.clientY - origin_y

						# add covers distance to div's position
						bg_el.css('left', (left + x) + 'px')
						bg_el.css('top', (top + y) + 'px')
					  
		.mouseup (e)->
			# set back to false
			mouse_down = false

			# set cursor back to default
			bg_el.css('cursor','auto')
			
d = new Drag_me(".wrapper")
d.make_dynamic()


### css for div
	.wrapper {
		height: 200px;
		border: 1px solid red;
		position: absolute;
		left: 0px;
		top: 0px;
	}
###



### extra info 
	mouse x,y coordinates 
	put "clientX:", e.clientX, "clientY:", e.clientY 

	same as clientX
	put "screenX:", e.screenX, "screenY:", e.screenY

	browsers height and width
	put "view.outerHeight:" , e.view.outerHeight, "view.outerWidth:" , e.view.outerWidth

	offset from x and y coordinates
	put "pageXOffset:" , e.view.pageXOffset, "pageYOffset:" , e.view.pageYOffset
###