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
+21.2k Golang : Convert(cast) string to rune and back to string example
+12.7k Golang : Exit, terminating or aborting a program
+6.9k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12.4k Golang : How to check if a string starts or ends with certain characters or words?
+19.6k Golang : Example for DSA(Digital Signature Algorithm) package functions
+8.8k Golang : Random integer with rand.Seed() within a given range
+12.6k Golang : HTTP response JSON encoded data
+6.3k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+27.3k Golang : Find files by name - cross platform example
+5.3k Responsive Google Adsense
+62.8k Golang : Convert HTTP Response body to string
+13.5k Golang : Read from buffered reader until specific number of bytes