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
+19.9k Golang : Get current URL example
+15.5k Golang : How to get Unix file descriptor for console and file
+14k Golang : Convert spaces to tabs and back to spaces example
+10.2k Golang : Test a slice of integers for odd and even numbers
+52.9k Golang : How to get struct field and value by name
+14.1k Golang : How to check if a file is hidden?
+14.2k Golang : convert rune to unicode hexadecimal value and back to rune character
+7k Golang : Decode XML data from RSS feed
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+6.7k Golang : Spell checking with ispell example
+9.8k Golang : Populate slice with sequential integers example