Home > Backend Development > Golang > How to modify file name in go language

How to modify file name in go language

青灯夜游
Release: 2022-12-23 09:04:24
Original
5838 people have browsed it

In the go language, you can use the Rename() function in the os package to modify the file name. The Rename() function is used to rename directories and files, and can also be used to move files. The syntax is "Rename (old file name, new file name)". In fact, the function is actually implemented using "syscall.Rename()", and then renamed through "MoveFile(from *uint16, to *uint16) (err error)=MoveFileW".

How to modify file name in go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

There is a Rename() function in the os package of Go language, which is used to rename directories and files. This function can also be used to move files.

The definition format of the Rename() function is:

func Rename(oldname, newname string) error
Copy after login

The input is the old file name, the new file name, and then an error is returned; in fact, the actual implementation of this function uses syscall.Rename( ) and then rename through

MoveFile(from *uint16, to *uint16) (err error) = MoveFileW

. The code is as follows:

package mainimport (
    "os")func main() {

    // 重命名文件
    file := `./测试文件.txt`
    err1 := os.Rename(file, `重命名文件.txt`)
    if err1 != nil {
        panic(err1)
    } else {
        println(`文件重命名成功`)
    }

    // 重命名文件夹
    folder := `./新建文件夹`
    err2 := os.Rename(folder, `重命名文件夹`)
    if err2 != nil {
        panic(err2)
    } else {
        println(`文件夹重命名成功`)
    }}
Copy after login

Expand knowledge: os package

The os package of Go language provides interfaces to operating system functions and is a relatively important package. As the name suggests, the os package is mainly used to perform basic system operations on the server, such as file operations, directory operations, execution of commands, signals and interrupts, processes, system status, etc.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to modify file name in go language. 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