Skip to content

Instantly share code, notes, and snippets.

@aki237
Created April 14, 2017 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aki237/ad57f247258f8e1e4ef6a7fc13013421 to your computer and use it in GitHub Desktop.
Save aki237/ad57f247258f8e1e4ef6a7fc13013421 to your computer and use it in GitHub Desktop.
// Loop and make 50 go routines or cncurrent function calls....
for count < 50 {
// Size (in no. of bytes) that has to be requested from the server
size := int64(bpc)
// If all that left is less than itself download the rest
// This generally happens in the last connection....
if total-size < size {
size = total
}
// Naming the temp file for the connection....
// Not necessary still little organised with numbers.
// Name the files with some random string and store them in the `files` array
num := fmt.Sprint(count + 1)
if count+1 < 10 {
num = fmt.Sprintf("0%s", num)
}
filename := filepath.Join("/tmp/", outfile, fmt.Sprintf("%s-%s", outfile, num))
files = append(files, filename)
// Open that temp file
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0755)
check err
// Add a Job to the Waitgroup
c.Add(1)
// This function will be described bottom....
go Out(f, c, count, from, size, written)
// Calculate the size left to be downloaded.
total -= size
// Calculate from which byte size the next request has to be made.
from += size + 1
// Increment the loop variable....
count++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment