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
+17.9k Golang : Iterate linked list example
+7.5k Golang : Individual and total number of words counter example
+23.1k Golang : Calculate time different
+6.8k Golang : Skip or discard items of non-interest when iterating example
+12.4k Golang : Print UTF-8 fonts on image example
+5.7k Javascript : How to refresh page with JQuery ?
+4.6k Java : Generate multiplication table example
+10.8k Golang : Get currencies exchange rates example
+14.9k Golang : Normalize unicode strings for comparison purpose
+19k Golang : Read input from console line
+10k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+13.5k Golang : Linear algebra and matrix calculation example