VOOZH about

URL: https://www.geeksforgeeks.org/go-language/how-to-rename-and-move-a-file-in-golang/

⇱ How to Rename and Move a File in Golang? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Rename and Move a File in Golang?

Last Updated : 2 Apr, 2020
In the Go language, you are allowed to rename and move the existing file to a new path with the help of the Rename() method. This method is used to rename and move a file from the old path to the new path.
  • If the given new path already exists and it is not in a directory, then this method will replace it. But OS-specific restrictions may apply when the given old path and the new path are in different directories.
  • If the given path is incorrect, then it will throw an error of type *LinkError.
  • It is defined under the os package so, you have to import os package in your program for accessing Remove() function.
Syntax:
func Rename(old_path, new_path string) error
Example 1: Output: Before: 👁 before renaming and moving a file in default directory in golang
After: 👁 after renaming and moving a file in default directory in golang
Example 2: Output: Before: 👁 before renaming and moving a file in default directory in golang
After: 👁 after renaming and moving a file in default directory in golang
Comment
Article Tags:

Explore