Last active
February 23, 2019 15:53
-
-
Save gaurav-gogia/1b5397f044272351cd3da06b96a86eda to your computer and use it in GitHub Desktop.
trying to copy a file concurrenlty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func copy() error { | |
src, size, err := open(dd) | |
if err != nil { | |
return err | |
} | |
defer src.Close() | |
dst, err := os.Create(dd) | |
if err != nil { | |
return err | |
} | |
defer func() { | |
dst.Sync() | |
dst.Close() | |
}() | |
w := NewCustomBuff(dst, dd.buffer) | |
_, err = io.Copy(w, src) | |
return err | |
} | |
func open(dd opts) (r io.ReadCloser, size int64, err error) { | |
r, err = os.Open(dd.src) | |
if err != nil { | |
return nil, 0, err | |
} | |
return r, size, nil | |
} | |
type CustomBuff struct { | |
w io.Writer | |
buf []byte | |
} | |
// NewFixedBuffer is a function | |
func NewCustomBuff(w io.Writer, size int64) *CustomBuff { | |
return &CustomBuff{ | |
w: w, | |
buf: make([]byte, size), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment