Renaming a file

The standard library provides a convenient function for moving a file. Renaming and moving are synonymous terms; if you want to move a file from one directory to another, use the os.Rename() function, as shown in the following code block:

package main 
 
import ( 
   "log" 
   "os" 
) 
 
func main() { 
   originalPath := "test.txt" 
   newPath := "test2.txt" 
   err := os.Rename(originalPath, newPath) 
   if err != nil { 
      log.Fatal(err) 
   } 
} 

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.