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
+21k Golang : Clean up null characters from input data
+33.7k Golang : convert(cast) bytes to string
+16.6k Golang : Gzip file example
+16.8k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+32.3k Golang : Copy directory - including sub-directories and files
+6.2k Golang : How to search a list of records or data structures
+13.9k Golang : convert rune to unicode hexadecimal value and back to rune character
+5.7k Unix/Linux : How to test user agents blocked successfully ?
+20.1k Golang : Count number of digits from given integer value
+21.9k Golang : Join arrays or slices example
+9.6k PHP : Get coordinates latitude/longitude from string
+6.6k Golang : Derive cryptographic key from passwords with Argon2