Created
June 20, 2018 20:45
-
-
Save rogerwelin/edcec23499830c8a24650fffa167302e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "io" | |
| "os" | |
| "path/filepath" | |
| "strconv" | |
| ) | |
| // test buffersizes; 1024, 1048576 and so on | |
| var BUFFERSIZE int64 | |
| func Copy(src, dst string, BUFFERSIZE int64) error { | |
| sourceFileStat, err := os.Stat(src) | |
| if err != nil { | |
| return err | |
| } | |
| if !sourceFileStat.Mode().IsRegular() { | |
| return fmt.Errorf("%s is not a regular file", src) | |
| } | |
| source, err := os.Open(src) | |
| if err != nil { | |
| return err | |
| } | |
| defer source.Close() | |
| _, err = os.Stat(dst) | |
| if err == nil { | |
| return fmt.Errorf("File %s already exists.", dst) | |
| } | |
| destination, err := os.Create(dst) | |
| if err != nil { | |
| return err | |
| } | |
| defer destination.Close() | |
| buf := make([]byte, BUFFERSIZE) | |
| for { | |
| n, err := source.Read(buf) | |
| if err != nil && err != io.EOF { | |
| return err | |
| } | |
| if n == 0 { | |
| break | |
| } | |
| // the buf[:n] notation allows you to read the first n characters from the buf slice | |
| if _, err := destination.Write(buf[:n]); err != nil { | |
| return err | |
| } | |
| } | |
| return err | |
| } | |
| func main() { | |
| if len(os.Args) != 4 { | |
| fmt.Printf("usage: %s source destination BUFFERSIZE\n", filepath.Base(os.Args[0])) | |
| os.Exit(1) | |
| } | |
| source := os.Args[1] | |
| destination := os.Args[2] | |
| BUFFERSIZE, _ = strconv.ParseInt(os.Args[3], 10, 64) | |
| fmt.Printf("Copying %s to %s\n", source, destination) | |
| err := Copy(source, destination, BUFFERSIZE) | |
| if err != nil { | |
| fmt.Printf("File copying failed: %q\n", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment