zoukankan      html  css  js  c++  java
  • go语言学习笔记----模拟实现文件拷贝函数

    实例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) }

      

  • 相关阅读:
    distributelist详细用法
    内电层与内电层分割基于AltiumDesigner
    PADS原理图中绘制网络标号NET LABEL
    [置顶] Bookmark
    视频设备接口
    Net Cable
    ARM入门建议
    基于Altium Designer的4层PCB板的绘制
    PADS中遇到的问题EMSYM昂信科技
    Altium Designer和PADS的功能大对比
  • 原文地址:https://www.cnblogs.com/saryli/p/11044683.html
Copyright © 2011-2022 走看看