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
+15.2k Golang : ROT47 (Caesar cipher by 47 characters) example
+11.4k Golang : Fuzzy string search or approximate string matching example
+14.9k Golang : How do I get the local IP (non-loopback) address ?
+8.4k Golang : Set or add headers for many or different handlers
+5.1k Golang : Get FX sentiment from website example
+20.6k Golang : Convert PNG transparent background image to JPG or JPEG image
+5.7k Facebook : How to force facebook to scrape latest URL link data?
+10.7k Nginx : TLS 1.2 support
+21.1k Curl usage examples with Golang
+30.6k Golang : Interpolating or substituting variables in string examples
+24.3k Golang : Time slice or date sort and reverse sort example
+9.5k Random number generation with crypto/rand in Go