To create a new directory using the os
package in Golang, you can use the Mkdir
function. Here's an example code snippet:
package main
import (
"fmt"
"os"
)
func main() {
dirName := "mydirectory"
err := os.Mkdir(dirName, 0755)
if err != nil {
fmt.Println("Error creating directory:", err)
} else {
fmt.Println("Directory created successfully")
}
}
In this code, the Mkdir
function is used to create a new directory with the specified name (mydirectory
). The second parameter is the permission mode, which determines the permissions of the new directory. In this example, 0755
is used, which gives read, write, and execute permissions to the owner, and read and execute permissions to others.
If the directory creation fails, the Mkdir
function returns an error, which you can handle accordingly.