Skip to content

Instantly share code, notes, and snippets.

@gaurav-gogia
Last active February 23, 2019 15:53
Show Gist options
  • Save gaurav-gogia/1b5397f044272351cd3da06b96a86eda to your computer and use it in GitHub Desktop.
Save gaurav-gogia/1b5397f044272351cd3da06b96a86eda to your computer and use it in GitHub Desktop.
trying to copy a file concurrenlty
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