rename a directory in go

To rename a directory in Go, you can use the os.Rename() function. The function takes two arguments: the old path of the directory and the new path of the directory.

Here's an example code that renames a directory named old_dir to new_dir:

main.go
package main

import (
	"fmt"
	"os"
)

func main() {
	oldPath := "/path/to/old_dir"
	newPath := "/path/to/new_dir"

	err := os.Rename(oldPath, newPath)
	if err != nil {
		fmt.Println(err)
	}
}
193 chars
17 lines

Make sure you have the appropriate permission to rename the directory. If the directory does not exist or if there's any other error, os.Rename() will return an error which you can handle accordingly.

gistlibby LogSnag