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
+14.1k Golang : Reverse IP address for reverse DNS lookup example
+10.6k Golang : Simple File Server
+5k Golang : Calculate a pip value and distance to target profit example
+12.3k Golang : List running EC2 instances and descriptions
+43.3k Golang : Convert []byte to image
+15.4k Golang : invalid character ',' looking for beginning of value
+4.7k JavaScript: Add marker function on Google Map
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+5.4k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+13.9k Golang : Get dimension(width and height) of image file
+6.3k Javascript : Generate random key with specific length
+14.4k Golang : How to convert a number to words