实例1
//main package main import ( "bufio" "flag" "fmt" "io" "os" "strings" ) func fileExists(filename string) bool { _, err := os.Stat(filename) return err == nil || os.IsExist(err) } func copyFile(src, dst string) (w int64, err error) { srcFile, err := os.Open(src) if err != nil { fmt.Println(err.Error()) return } defer srcFile.Close() dstFile, err := os.Create(dst) if err != nil { fmt.Println(err.Error()) return } defer dstFile.Close() return io.Copy(dstFile, srcFile) } func copyFileAction(src, dst string, showProgress, force bool) { if !force { if fileExists(dst) { fmt.Printf("%s exists override? y/n ", dst) reader := bufio.NewReader(os.Stdin) data, _, _ := reader.ReadLine() if strings.TrimSpace(string(data)) != "y" { return } } } copyFile(src, dst) if showProgress { fmt.Printf("'%s'->'%s' ", src, dst) } } func main() { var showProgress, force bool
//定义命令行参数 flag.BoolVar(&force, "f", false, "force copy when existing") flag.BoolVar(&showProgress, "v", false, "explain what is being done") flag.Parse() //非法命令行数量检测 if flag.NArg() < 2 { flag.Usage() return }
//取第0个参数与第一个参数作为源文件名跟目标文件名 copyFileAction(flag.Arg(0), flag.Arg(1), showProgress, force) }