Hands-On System Programming with Go
上QQ阅读APP看书,第一时间看更新

Move

The os.Rename function makes it possible to change a file name and/or its directory. Note that this operation replaces the destination file if it already exists.

The code for changing a file's name or its directory is as follows:

import "os"

func main() {
if err := os.Rename("file.txt", "../file.txt"); err != nil {
fmt.Println("Error:", err)
}
}