How to Rename and Move a File in Golang? Last Updated : 02 Apr, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: C // Go program to illustrate how to rename // and move a file in default directory package main import ( "log" "os" ) func main() { // Rename and Remove a file // Using Rename() function Original_Path := "GeeksforGeeks.txt" New_Path := "gfg.txt" e := os.Rename(Original_Path, New_Path) if e != nil { log.Fatal(e) } } Output: Before: After: Example 2: C // Go program to illustrate how to rename // and remove a file in the new directory package main import ( "log" "os" ) func main() { // Rename and Remove a file // Using Rename() function Original_Path := "/Users/anki/Documents/new_folder/GeeksforGeeks.txt" New_Path := "/Users/anki/Documents/new_folder/myfolder/gfg.txt" e := os.Rename(Original_Path, New_Path) if e != nil { log.Fatal(e) } } Output: Before: After: Comment More infoAdvertise with us Next Article How to Uncompress a File in Golang? A ankita_saini Follow Improve Article Tags : Go Language Golang-File-Handling Similar Reads How to Delete or Remove a File in Golang? In the Go language, you are allowed to remove the existing file with the help of the Remove() method. This method removes the specified file from the director or it also removes empty directories. If the given path is incorrect, then it will throw an error of type *PathError. It is defined under the 1 min read How to Read a CSV File in Golang? Golang offers a vast inbuilt library that can be used to perform read and write operations on files. To read a CSV file, the following methods are used in Golang: os.Open(): The os.Open() method opens the named file for reading. This method returns either the os.File pointer or an error. encoding/cs 4 min read How to Read and Write the Files in Golang? Golang offers a vast inbuilt library that can be used to perform read and write operations on files. In order to read from files on the local system, the io/ioutil module is put to use. The io/ioutil module is also used to write content to the file. This revised version reflects the changes made in 4 min read How to Uncompress a File in Golang? As per Wikipedia, data compression or file compression can be understood as a process of reducing the size of a particular file/folder/whatever data while still preserving the original data. Lesser file size has a lot of benefits as it will then occupy lesser storage area giving you more space for o 6 min read How to Check File Size and Last Modified Time in Golang? In the Go language, the os module provides the ability to access native operating-system features. In the os package, the Stat() function is used to check the file size and last modified time. This method gives the FileInfo structure describing the file and an error of type *PathError. // FileInfo d 2 min read How to Read File Word By Word in Golang? File reading is such an important aspect of a programming language. We need to perform certain operations programmatically, using automation in file reading/writing allows us to create certain programs/projects which might have looked impossible otherwise. File InputTo work with files, we have to in 5 min read Like