mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 17:36:12 +08:00
update book code
This commit is contained in:
29
eBook/examples/chapter_12/filecopy.go
Normal file
29
eBook/examples/chapter_12/filecopy.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// filecopy.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
CopyFile("target.txt", "source.txt")
|
||||
fmt.Println("Copy done!")
|
||||
}
|
||||
|
||||
func CopyFile(dstName, srcName string) (written int64, err error) {
|
||||
src, err := os.Open(srcName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
return io.Copy(dst, src)
|
||||
}
|
||||
Reference in New Issue
Block a user