Golang : Move file to another directory
So far, Go does not have a function to directly move file to different folders. We have to provide our own method to do that.
First way is to use the os.Rename method. We are going to move a file \Folder_A\file.txt
to \Folder_B\file.txt
movefile.go
package main
import (
"fmt"
"os"
)
func main() {
err := os.Rename("\Folder_A\file.txt", "\Folder_B\file.txt")
if err != nil {
fmt.Println(err)
return
}
}
the second method is Copy file to target destination and Delete file at source. It was covered in this Copy file in Go tutorial. All you need to do is to specify the target destination and delete the file at source after copying.
See also : Golang : Rename file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+23.8k Golang : Find biggest/largest number in array
+28.2k Golang : Read, Write(Create) and Delete Cookie example
+23.6k Golang : Use regular expression to validate domain name
+36.1k Golang : Validate IP address
+9.9k Golang : How to get quoted string into another string?
+9.4k Golang : Detect number of active displays and the display's resolution
+15k Golang : Get all local users and print out their home directory, description and group id
+36.1k Golang : Convert(cast) int64 to string
+12.8k Golang : How to calculate the distance between two coordinates using Haversine formula
+5.7k Golang : Extract unicode string from another unicode string example
+25k Golang : Get current file path of a file or executable
+9.1k Golang : Launch Mac OS X Preview (or other OS) application from your program example