
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rename a Specified File by Another Name in Go
Rename() is one of the internal function of golang used for renaming a specific file. Here we have used three examples in the first one we are using the Rename() function present in os package while in the second example we are using TempFile() function present in the ioutil package respectively.
Method 1: Using OS Package
In this method, we will write a go language program to rename a specified file by another name by using the Rename() function present in os package.
Syntax
func Rename(oldpath, newpath string) error
The Rename() function is present in os package and is used to rename a particular file present in the given directory. The function takes two parameters, the first one is the old path of the file and the second one is the new path for the file. The function returns an error if the renaming operation fails.
Algorithm
First, we need to import the fmt and os package.
Now, start the main() function. Inside the main() initialize a variable and store in it the old and new paths of the file to be renamed.
Call the Rename() function from the os package, passing the old path and the new path as arguments
Check the return value of the Rename() function, if it's not nil, print an error message indicating the error, otherwise print a success message that the file is successfully renamed.
Example
In this example, we are going to use the os Package of Golang to rename a specified file by another name
package main import ( "fmt" "os" ) func main() { oldPath := "newFile.txt" newPath := "renamed.txt" err := os.Rename(oldPath, newPath) if err != nil { fmt.Println("Error renaming file:", err) } else { fmt.Println("File renamed successfully") } }
Output
File renamed successfully
Method 2: Using ioutil Package
In this method, we will write a go language program to remove a specified file by another name by using the Tempfile() function present in ioutil package.
Algorithm
First, we need to import the "ioutil", "fmt" and "os" package.
Then, start the main() function. Inside the main() function initialize two variables and store in them the paths of the old file to be renamed and new path where the path needs to be saved.
Call the TempFile() function from the ioutil package, passing the directory where the file is located as the first argument and a prefix for the temporary file name as the second argument
Check the return value of the TempFile() function, if it's not nil, print an error message indicating the error.
Close the returned file.
Copy the content of the old file to the temporary file.
Remove the old file using the os.Remove() function. Rename the temporary file to the new file using the os.Rename() function.
Check the return value of the os.Rename() function, if it's not nil, print an error message indicating the error, else print a success message.
Return any error that might have occurred.
Example
This example creates a temporary file using the ioutil.TempFile() function, copies the content of the old file to the temporary file using the ioutil.ReadFile() and ioutil.WriteFile() functions, removes the old file using the os.Remove() function, and renames the temporary file to the new file using the os.Rename() function. If any error occurs, it is reported and the program terminates. If the renaming process is successful, it prints a message indicating so.
package main import ( "fmt" "io/ioutil" "os" ) func main() { oldPath := "pad.txt" newPath := "newfile.txt" // Create a temporary file tempFile, err := ioutil.TempFile("", "tempfile") if err != nil { fmt.Println("Error creating temporary file:", err) return } // Close the temporary file err = tempFile.Close() if err != nil { fmt.Println("Error closing temporary file:", err) return } // Copy the content of the old file to the temporary file oldData, err := ioutil.ReadFile(oldPath) if err != nil { fmt.Println("Error reading old file:", err) return } err = ioutil.WriteFile(tempFile.Name(), oldData, 0666) if err != nil { fmt.Println("Error writing to temporary file:", err) return } // Remove the old file err = os.Remove(oldPath) if err != nil { fmt.Println("Error removing old file:", err) return } // Rename the temporary file to the new file err = os.Rename(tempFile.Name(), newPath) if err != nil { fmt.Println("Error renaming temporary file:", err) return } fmt.Println("File renamed successfully") }
Output
File renamed successfully
Conclusion
In Conclusion, the os package and the exec package are two different methods to rename a specified file in Go. The os package provides a Rename() function that can be used to rename a file, while the exec package can be used to execute shell commands to rename a file. Both methods are effective and can be used based on the requirements and preferences of the developer.