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.7k Golang : Detect user location with HTML5 geo-location
+5.1k Golang : Debug with Godebug
+18.5k Android Studio : AlertDialog and EditText to get user string input example
+7.9k Golang : Find the length of big.Int variable example
+12.5k Golang : Reverse IP address for reverse DNS lookup example
+7k Golang : Qt splash screen with delay example
+5.2k Golang : Create new color from command line parameters
+4k HTTP common errors and their meaning explained
+11k Android Studio : Highlight ImageButton when pressed on example
+8.9k Golang : Use regular expression to get all upper case or lower case characters example
+15.8k Golang : XML to JSON example
+3.7k Golang : How to pass data between controllers with JSON Web Token