To create a single listing in Go, use the os.Mkdir()
perform. If you wish to create a hierarchy of folders (nested directories), use os.MkdirAll()
. Each features require a path and permission bits of the folder as arguments.
Within the examples beneath, we use the
os.ModePerm
fixed as permission bits which is equal to0777
. For directories in Unix-like programs, it signifies that a person has rights to checklist, modify and search recordsdata within the listing.
Create a single listing
package deal foremost
import (
"log"
"os"
)
func foremost() {
if err := os.Mkdir("a", os.ModePerm); err != nil {
log.Deadly(err)
}
}
The os.Mkdir()
creates a brand new listing with the desired identify, however can not create subdirectories. For instance, if you happen to use os.Mkdir()
with "a/b/c/d"
as a identify
argument:
os.Mkdir("a/b/c/d", os.ModePerm)
you get the error:
mkdir a/b/c/d: no such file or listing
Create a hierarchy of directories (nested directories)
package deal foremost
import (
"log"
"os"
)
func foremost() {
if err := os.MkdirAll("a/b/c/d", os.ModePerm); err != nil {
log.Deadly(err)
}
}
The os.MkdirAll()
creates a given listing, together with any mandatory guardian folder. Use this perform if it is advisable create a nested hierarchy of directories in your program.