go-examples/go-cp/main.go
2017-07-07 14:29:36 +02:00

52 lines
872 B
Go

package main
import "fmt"
import "os"
import "log"
import "io"
var err error
var from, to *os.File
const bufSize = 64 * 1024
func copyFile(src io.Reader, dst io.Writer) error {
buf := make([]byte, bufSize)
for {
n, er := src.Read(buf)
if er != nil || n == 0 {
if er != nil && er != io.EOF {
return fmt.Errorf("Read error: %v:", er)
}
break
}
_, wer := dst.Write(buf[:n])
if wer != nil {
return fmt.Errorf("Write error: %v:", wer)
}
}
return nil
}
func main() {
if len(os.Args) != 3 {
log.Fatalln("Usage: go-cp from to")
}
from, err = os.Open(os.Args[1])
if err != nil {
log.Fatalln("Error open", err)
}
defer from.Close()
to, err = os.Create(os.Args[2])
if err != nil {
log.Fatalln("Error open", err)
}
defer to.Close()
// err = copyFile(from, to)
_, err = io.Copy(from, to)
if err != nil {
log.Fatalln(err)
}
}