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
+5.2k Golang : When to use make or new?
+10.1k Golang : Simple client-server HMAC authentication without SSL example
+14.4k Golang : Get sub string example
+2.8k Detect if Google Analytics and Developer Media are loaded properly or not
+39.5k Golang : Convert string to array/slice
+2.3k Java : Get FX sentiment from website example
+14.7k Golang : How to remove certain lines from a file
+20.3k Golang : Read directory content with filepath.Walk()
+3.9k Golang : How to deal with configuration data?
+16.9k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+18.4k Golang : How to read float value from standard input ?
+21.4k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date