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
+8.2k Golang : Allow Cross-Origin Resource Sharing request
+5.2k Golang : How to call function inside template with template.FuncMap
+6.7k Golang : Metaprogramming example of wrapping a function
+19.9k Golang : Set and Get HTTP request headers example
+17.1k Golang : Clearing slice
+3.8k Golang : The Tao of importing package
+7.1k Golang : Take screen shot of browser with JQuery example
+3.5k Google : Block or disable caching of your website content
+4.6k Golang : Convert an executable file into []byte example
+12.5k Golang : Find location by IP address and display with Google Map
+5.9k Golang : Shuffle strings array
+8.2k Golang : Fix go.exe is not compatible with the version of Windows you're running