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
+10.4k Golang : Convert file unix timestamp to UTC time example
+24.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+12.6k Golang : "https://" not allowed in import path
+8.1k Golang : What fmt.Println() can do and println() cannot do
+10.4k Golang : Detect number of faces or vehicles in a photo
+14.7k Golang : How to get URL port?
+18.4k Golang : Put UTF8 text on OpenCV video capture image frame
+8.8k Golang : Find duplicate files with filepath.Walk
+18.7k Golang : Send email with attachment
+20.4k Golang : Compare floating-point numbers
+20.8k Nginx + FastCGI + Go Setup.
+7.6k Golang : Rename part of filename