Home > Backend Development > Golang > How to copy or move files using Golang?

How to copy or move files using Golang?

WBOY
Release: 2024-06-05 13:46:56
Original
847 people have browsed it

In Golang, you can use the os package to copy or move files: Copy files: Use io.Copy to copy the contents of the source file to the target file. Moving files: Use os.Rename to rename a source file to a target file, essentially moving the file.

如何使用 Golang 复制或移动文件?

How to copy or move files using Golang?

In Golang, files can be copied or moved by using the os package. Here is a code example of how to do it:

1. Copy file

package main

import (
    "io"
    "os"
)

func main() {
    srcFile, err := os.Open("source.txt")
    if err != nil {
        panic(err)
    }
    defer srcFile.Close()

    dstFile, err := os.Create("destination.txt")
    if err != nil {
        panic(err)
    }
    defer dstFile.Close()

    _, err = io.Copy(dstFile, srcFile)
    if err != nil {
        panic(err)
    }
}
Copy after login

2. Move file

package main

import (
    "os"
)

func main() {
    err := os.Rename("source.txt", "destination.txt")
    if err != nil {
        panic(err)
    }
}
Copy after login

Practical Case

In actual use, you can use the following code example to copy or move files:

package main

import (
    "context"
    "fmt"
    "io"
    "io/ioutil"
    "os"
)

func copyFile(src, dst string) error {
    srcFile, err := os.Open(src)
    if err != nil {
        return err
    }
    defer srcFile.Close()

    dstFile, err := os.Create(dst)
    if err != nil {
        return err
    }
    defer dstFile.Close()

    if _, err := io.Copy(dstFile, srcFile); err != nil {
        return err
    }

    return nil
}

func moveFile(src, dst string) error {
    if err := copyFile(src, dst); err != nil {
        return err
    }
    return os.Remove(src)
}

func main() {
    srcData := "Hello World!"
    err := ioutil.WriteFile("source.txt", []byte(srcData), 0644)
    if err != nil {
        panic(err)
    }

    // 复制文件
    if err := copyFile("source.txt", "destination1.txt"); err != nil {
        panic(err)
    }

    // 移动文件
    if err := moveFile("destination1.txt", "destination2.txt"); err != nil {
        panic(err)
    }

    // 检查是否成功
    dstData, err := ioutil.ReadFile("destination2.txt")
    if err != nil {
        panic(err)
    }
    fmt.Println(string(dstData)) // 输出:"Hello World!"
}
Copy after login

The above is the detailed content of How to copy or move files using Golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template