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.5k Golang : Convert int(year) to time.Time type
+13.6k Golang : Compress and decompress file with compress/flate example
+6.4k Golang : Check if password length meet the requirement
+19.7k Swift : Convert (cast) Int to int32 or Uint32
+19.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+28k Golang : Change a file last modified date and time
+9.4k Golang : interface - when and where to use examples
+11.5k Golang : convert(cast) float to string
+13.8k Golang : Fix image: unknown format error
+5.7k PHP : Get client IP address
+7.7k Golang : Grayscale Image