To rename a file using the os
package in Golang, you can use the Rename
function.
Here is an example:
package main
import (
"fmt"
"os"
)
func main() {
oldName := "old_file.txt"
newName := "new_file.txt"
err := os.Rename(oldName, newName)
if err != nil {
fmt.Println("Error renaming file:", err)
return
}
fmt.Println("File renamed successfully.")
}
In this example, we specify the old name of the file as "old_file.txt" and the new name as "new_file.txt". The os.Rename
function is used to rename the file. If there is any error during the rename operation, an error message is printed. Otherwise, a success message is printed.