file copied from download folder to desktop
//cmd
C:\Users\bob\golang7>go run main.go
Bytes Written: 25698304
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
)
// Copy a file
func main() {
// Open original file
originalFilepath := "C:\\Users\\bob\\Downloads\\mysql-installer-web-community-8.0.21.0.msi"
_, filename := filepath.Split(originalFilepath)
original, err := os.Open(originalFilepath)
if err != nil {
log.Fatal(err)
}
defer original.Close()
// Create new file
newFilepath := filepath.Join("c:\\Users\\bob\\Desktop", filename)
new, err := os.Create(newFilepath)
if err != nil {
log.Fatal(err)
}
defer new.Close()
//This will copy
bytesWritten, err := io.Copy(new, original)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Bytes Written: %d\n", bytesWritten)
}
reference:
io.copy
filepath
No comments:
Post a Comment