The way to implement file segmentation in Go language _golang

Source: Internet
Author: User
Tags file info

This article illustrates the way to implement file segmentation in Go language. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
Import (
Import "StrConv"
var infile *string = flag. String ("F", "Null", "Please input a file name or dir.")
var size *string = flag. String ("s", "0", "Please input a DST file size.")
Func datacopy (File *os. file, file *os. File)
Func splitfile (File *os. File, size int) {
Finfo, Err: = file. Stat ()
If Err!= nil {
Fmt. Println ("Get file info failed:", file, size)
Fmt. Println (finfo, size)
Up to 1m per copy
BufSize: = 1024 * 1024
If size < BufSize {
bufsize = Size
BUF: = Make ([]byte, BufSize)
num: = (int (finfo). Size ()) + size-1)/size
Fmt. Println (num, Len (BUF))
For I: = 0; i < num; i++ {
Copylen: = 0
NewFileName: = Finfo. Name () + StrConv. Itoa (i)
NewFile, ERR1: = OS. Create (NewFileName)
If err1!= Nil {
Fmt. Println ("Failed to create file", NewFileName)
} else {
Fmt. Println ("Create File:", NewFileName)
For Copylen < size {
N, err2: = file. Read (BUF)
If err2!= nil && err2!= io. EOF {
Fmt. Println (ERR2, "failed to read from:", file)
If n <= 0 {
Fmt. PRINTLN (n, Len (BUF))
Write a file
W_buf: = Buf[:n]
NewFile. Write (W_BUF)
Copylen + N

Func Main () {
Flag. Parse ()
if *infile = = "Null" {
Fmt. Println ("No file to input")
File, err: = OS. Open (*infile)
If Err!= nil {
Fmt. Println ("Failed to open:", *infile)
Defer file. Close ()
Size, _: = StrConv. Atoi (*size)
Splitfile (file, size*1024)

I hope this article will help you with your go language program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.