Go list directory
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.
Directory definition
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.
$ go version go version go1.22.2 linux/amd64
We use Go version 1.22.2.
Go create directory with os.Mkdir
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.
Go create multiple directories with os.MkdirAll
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.
Go get current working directory with os.Getwd
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.
Go check if directory exists
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.
Go rename directory with os.Rename
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
.
Go delete directory with os.Remove
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.
Go delete directory and its contents with os.RemoveAll
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.
Go list directory with filepath.Glob
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.
Source
In this article we have worked with directories in Go.
Author
List all Go tutorials.