Move a file from one location to another in Golang


os.Rename() can also move file from one location to another at same time renaming file name.

Example

package main
 
import (
	"log"
	"os"
)
 
func main() {
	oldLocation := "/var/www/html/test.txt"
	newLocation := "/var/www/html/src/test.txt"
	err := os.Rename(oldLocation, newLocation)
	if err != nil {
		log.Fatal(err)
	}
}
Most Helpful This Week