Home Backend Development Golang Rename files using Go's Rename function

Rename files using Go's Rename function

Apr 08, 2024 pm 02:21 PM
go Rename file Keyword extraction

The os.Rename function in the Go language can conveniently rename files or directories and update file or directory names without losing data. It takes two parameters: oldpath (current path) and newpath (new path). This function overwrites existing targets and can only rename files or directories in the same file system.

利用 Go 语言的 Rename 函数重命名文件

Use the Rename function of Go language to rename files

Introduction

Go The language's os.Rename function allows you to easily rename a file or directory. It provides a safe way to update the name of a file or directory without losing any data.

Syntax

func Rename(oldpath, newpath string) error
Copy after login
  • oldpath: The current path of the file to be renamed.
  • newpath: New file path.

Practical case

The following code snippet demonstrates how to use the Rename function to rename a file:

package main

import (
    "fmt"
    "os"
)

func main() {
    err := os.Rename("file.txt", "new_file.txt")
    if err != nil {
        fmt.Println("Error renaming file:", err)
        return
    }
    fmt.Println("File renamed successfully")
}
Copy after login

above In the example, we rename the file named "file.txt" to "new_file.txt". If the renaming is successful, the program will output "File renamed successfully". Otherwise, it will print an error message.

Note

  • Rename function will overwrite existing files or directories. If the target path already exists, it will be overwritten by the renamed file or directory.
  • If oldpath and newpath point to the same file, Rename will do nothing.
  • Rename Will not rename across file systems. It can only rename files or directories in the same file system.

The above is the detailed content of Rename files using Go's Rename function. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send Go WebSocket messages? How to send Go WebSocket messages? Jun 03, 2024 pm 04:53 PM

How to send Go WebSocket messages?

How to avoid memory leaks in Golang technical performance optimization? How to avoid memory leaks in Golang technical performance optimization? Jun 04, 2024 pm 12:27 PM

How to avoid memory leaks in Golang technical performance optimization?

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

How to match timestamps using regular expressions in Go?

The difference between Golang and Go language The difference between Golang and Go language May 31, 2024 pm 08:10 PM

The difference between Golang and Go language

A guide to unit testing Go concurrent functions A guide to unit testing Go concurrent functions May 03, 2024 am 10:54 AM

A guide to unit testing Go concurrent functions

Golang framework documentation best practices Golang framework documentation best practices Jun 04, 2024 pm 05:00 PM

Golang framework documentation best practices

How to create a prioritized Goroutine in Go? How to create a prioritized Goroutine in Go? Jun 04, 2024 pm 12:41 PM

How to create a prioritized Goroutine in Go?

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How does Go WebSocket integrate with databases?

See all articles