美文网首页
golang copy

golang copy

作者: 司空儿 | 来源:发表于2020-11-13 16:05 被阅读0次
package main

import (

    "fmt"

    "io"

    "os"

    "path/filepath"

    "strconv"

)

var BUFFERSIZE int64

func Copy1(src, dst string, BUFFERSIZE int64) error {

    sourceFileStat, err := os.Stat(src)

    if err != nil {

        return err

    }

    if !sourceFileStat.Mode().IsRegular() {

        return fmt.Errorf("%s is not a regular file", src)

    }

    source, err := os.Open(src)

    if err != nil {

        return err

    }

    defer source.Close()

    _, err = os.Stat(dst)

    if err == nil {

        return fmt.Errorf("file %s already exists", dst)

    }

    destination, err := os.Create(dst)

    if err != nil {

        return err

    }

    defer destination.Close()

    if err != nil {

        panic(err)

    }

    buf := make([]byte, BUFFERSIZE)

    for {

        n, err := source.Read(buf)

        if err != nil && err != io.EOF {

            return err

        }

        if n == 0 {

            break

        }

        if _, err := destination.Write(buf[:n]); err != nil {

            return err

        }

    }

    return err

}

func Copy(src, dst string) (int64, error) {

    sourceFileStat, err := os.Stat(src)

    if err != nil {

        return 0, err

    }

    if !sourceFileStat.Mode().IsRegular() {

        return 0, fmt.Errorf("%s is not a regular file", src)

    }

    source, err := os.Open(src)

    if err != nil {

            return 0, err

    }

    defer source.Close()

    destination, err := os.Create(dst)

    if err != nil {

        return 0, err

    }

    defer destination.Close()

    nBytes, err := io.Copy(destination, source)

    return nBytes, err

}

func main() {

    if len(os.Args) != 4 {

        fmt.Printf("usage: %s source destination BUFFERSIZE\n",

            filepath.Base(os.Args[0]))

        os.Exit(1)

    }

    source := os.Args[1]

    destination := os.Args[2]

    BUFFERSIZE, _ = strconv.ParseInt(os.Args[3], 10, 64)

    fmt.Printf("Copying %s to %s\n", source, destination)

    err := Copy1(source, destination, BUFFERSIZE)

    if err != nil {

        fmt.Printf("File copying failed: %q\n", err)

    }

}


作者:holdtom
链接:https://www.imooc.com/article/271446
来源:慕课网

相关文章

  • golang copy

    golang copy函数用于在两个slice之间进行拷贝数据,其拷贝数据的长度为 len(dst)与len(sr...

  • golang copy

  • golang-101-hacks(15)——copy

    注:本文是对golang-101-hacks中文翻译。内置函数copy定义如下: func copy(dst, s...

  • golang实现文件copy

  • golang copy 函数的使用

    之前用 copy 不多,本以为它是个很方便的函数,没想到在做练习题时竟还是被它坑了。是我对他期望太多了。 基本认识...

  • no copy

    no copy 机制 转载 Golang技术分享 专注于Go语言的知识分享 小菜刀读Go源码时,发现一个高频注释语...

  • 2019-10-16 Golang copy struct

    The go is passing by value.That is, if I doa = ba is a co...

  • Copy Copy Copy

    说到Copy 大多会想到浅复制(指针复制),深复制(内容复制)。看一张图: 你会发现对于集合类型,深复制那写的是...

  • 2018-09-27

    copy的问题 copy.copy返回浅copy copy.deepcopy返回深copy 1fromcopyim...

  • 关于深copy和浅copy

    深copy 所谓深copy是指内容copy 浅copy 所谓浅copy是指指针copy 集合的单层深copy 在多...

网友评论

      本文标题:golang copy

      本文链接:https://www.haomeiwen.com/subject/scxbbktx.html