last modified April 11, 2024
In this article we show how to work with directories in Golang. We create directories, delete them, rename them, and list their contents.
A directory is a unit in a computer's file system for storing and locating files. Directories are hierarchically organized into a tree. Directories have parent-child relationships. A directory is sometimes also called a folder.
We work with the os and filepath packages.
The os.Mkdir creates a new directory with the specified name and
permission bits.
package main
import (
"log"
"os"
)
func main() {
err := os.Mkdir("tmp", 0755)
if err != nil {
log.Fatal(err)
}
}
The example creates a directory named tmp. The 0755
means read and execute access for everyone and also write access for the owner
of the file.
The MkdirAll function creates a directory, along with any necessary
parents, and returns nil, or else returns an error. The permission
bits are used for all directories that the function creates. If the directory
already exists, MkdirAll does nothing and returns nil.
package main
import (
"fmt"
"log"
"os"
)
func main() {
path := "tmp/doc/new"
err := os.MkdirAll(path, 0755)
if err != nil {
log.Fatal(err)
}
fmt.Println("directory created")
}
In the code example, we create a directory called new along with its
parents.
The os.Getwd returns a rooted path name corresponding to the
current directory.
package main
import (
"fmt"
"log"
"os"
)
func main() {
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
fmt.Println(path)
}
The example prints the current working directory. It is the directory in which we launch the program.
The IsNotExist returns a boolean value indicating whether the error
is known to report that a file or directory does not exist.
package main
import (
"fmt"
"os"
)
func main() {
path := "tmp"
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0755)
fmt.Println("Directory created")
} else {
fmt.Println("Directory already exists")
}
}
In the code example, we create a directory if it does not exists. If it exists, we print a message.
if _, err := os.Stat(path); os.IsNotExist(err) {
First, we use the Stat function, which returns the FileInfo
structure describing file. If this function returns an error, we use the
IsNotExist to determine if the error is caused by the fact that
the directory already exists.
The Rename function renames (moves) a source to a destination.
package main
import (
"fmt"
"log"
"os"
)
func main() {
oldpath := "tmp"
newpath := "tmp2"
err := os.Rename(oldpath, newpath)
if err != nil {
log.Fatal(err)
}
fmt.Println("directory renamed")
}
The example renames a directory tmp to tmp2.
With the os.Remove function, we remove a single directory; the
directory must be empty.
package main
import (
"log"
"os"
)
func main() {
err := os.Remove("tmp")
if err != nil {
log.Fatal(err)
}
}
The example removes an empty tmp directory.
The RemoveAll removes the directory and its contents recursively.
package main
import (
"log"
"os"
)
func main() {
err := os.RemoveAll("tmp")
if err != nil {
log.Fatal(err)
}
}
The example deletes the tmp directory and all its files and
subdirectories.
The filepath.Glob returns the names of all files matching pattern
or nil if there is no matching file.
func Glob(pattern string) (matches []string, err error)
This is the syntax of the filepath.Glob function.
package main
import (
"fmt"
"log"
"path/filepath"
)
func main() {
files, err := filepath.Glob("/home/janbodnar/Documents/prog/golang/**/*.go")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file)
}
}
The example lists all Go files in the given directory. With the **
pattern, the listing is recursive.
In this article we have worked with directories in Go.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Go tutorials.