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
+6.1k Web : How to see your website from different countries?
+4.8k Unix/Linux : Get reboot history or check when was the last reboot date
+12.9k Elastic Search : Mapping date format and sort by date
+3.8k JavaScript: Add marker function on Google Map
+3.7k Chrome : How to block socketloop.com links in Google SERP?
+6.6k Golang : Convert(cast) io.Reader type to string
+22.4k Golang : Use regular expression to validate domain name
+22.4k Golang : Check if element exist in map
+7k Swift : Convert (cast) String to Float
+13.3k Golang : How to filter a map's elements for faster lookup
+45.5k Golang : Convert int to byte array([]byte)
+17.7k Golang : Delete duplicate items from a slice/array