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
+9.4k Golang : Underscore string example
+15.8k Golang : XML to JSON example
+4.2k Unix/Linux : secure copying between servers with SCP command examples
+5.3k Golang : Extract sub-strings
+8k Golang : Generate random Chinese, Japanese, Korean and other runes
+5k Golang : How to write backslash in string?
+8.6k Golang : Eroding and dilating image with OpenCV example
+4.1k Golang : micron to centimeter example
+12.8k Golang : Simple word wrap or line breaking example
+32.3k Golang : Create x509 certificate, private and public keys
+5k Facebook : How to force facebook to scrape latest URL link data?
+10.6k Golang : Handle API query by curl with Gorilla Queries example