Home > Article > Backend Development > How to modify file name in go language
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".
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
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
. 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(`文件夹重命名成功`) }}
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!