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
+12.1k Golang : Detect user location with HTML5 geo-location
+28.2k Golang : Connect to database (MySQL/MariaDB) server
+18.4k Golang : Example for RSA package functions
+4.7k Golang : How to pass data between controllers with JSON Web Token
+8.8k Golang : HTTP Routing with Goji example
+9.6k Golang : How to generate Code 39 barcode?
+5.4k Python : Delay with time.sleep() function example
+7.9k Golang : Trim everything onward after a word
+5.2k PHP : See installed compiled-in-modules
+7.8k Golang : Reverse a string with unicode
+11.6k Golang : Simple file scaning and remove virus example
+8.1k Golang : How To Use Panic and Recover