Golang : Move file to another directory
Tags : golang move-file
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
Tags : golang move-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
+2.1k Golang : What is StructTag and how to get StructTag's value?
+3.7k Random number generation with crypto/rand in Go
+1.8k Fix sudo yum hang problem with no output or error messages
+2.2k Golang : Handle sub domain with Gin
+1.9k Golang : Detect sample rate, channels or latency with PortAudio
+2k Golang : Muxing with Martini example
+1.4k Golang : Totalize or add-up an array or slice example
+6.7k Golang : Find files by name - cross platform example
+3.5k Golang : Resolve domain name to IP4 and IP6 addresses.
+4.3k Golang : Overwrite previous output with count down timer
+1.4k Chrome : How to block socketloop.com links in Google SERP?
+2.7k Golang : Load ASN1 encoded DSA public key PEM file example