
Golang's Walk Function is a powerful tool that allows you to iterate over a data structure, such as a map or a slice, and perform an action on each element.
It's a flexible and efficient way to process data, and we'll explore its capabilities in this tutorial.
The Walk function takes three arguments: the data structure to iterate over, a function to apply to each element, and a pointer to a value that will be set to the element's key or index.
This allows you to easily access and manipulate the data in your Go program.
Related reading: Golang Iterate Map
Go File Implementation
Go's native walk implementation is indeed the slowest among the alternatives, taking around 1.4 seconds to scan a directory with 67359 files, as seen in a comparison with other implementations.
The fastest way to get all the names of all files in a directory using Go is to use the godirwalk library, which is at least 2x as fast as the next quickest implementation, cwalk or walk.
godirwalk achieves this speed by allowing the "unsorted" option, which can pull out an additional ~150ms of speed, making it the best option if bleeding performance matters.
For your interest: Golang Go
Go File Implementation Comparison
The native Go file implementation is indeed the slowest among the options tested. It took around 1.3 seconds to scan a directory with 67359 files.
The fastest way to get all the names of all files in a directory using Go is with the godirwalk library, which is at least 2x as fast as the next quickest implementation. It took around 300ms to scan the same directory.
Ripgrep, a disk scanner, is the fastest option but it ignores certain directories and took around 600ms to scan the directory. This is not a fair comparison, but it gives an idea of the upper bounds of useful performance.
The godirwalk library is the best option if bleeding performance matters, but if you need a drop-in replacement for some additional speed, cwalk or walk might be a better choice.
You might enjoy: Go vs Golang
Func Is Local in Go 1.20
In Go 1.20, a new function called IsLocal was added to help developers determine if a path is local or not. It's a purely lexical operation, meaning it only checks the path's syntax and doesn't account for symbolic links in the filesystem.
To determine if a path is local, IsLocal checks four conditions: it must be within the subtree rooted at the directory in which the path is evaluated, it must not be an absolute path, it must not be empty, and on Windows, it must not be a reserved name such as "NUL".
Here are the conditions that IsLocal checks in more detail:
- Within the subtree rooted at the directory in which the path is evaluated
- Not an absolute path
- Not empty
- On Windows, not a reserved name such as "NUL"
If IsLocal returns true, Join(base, path) will always produce a path contained within base, and Clean(path) will always produce an unrooted path with no ".." path elements.
Optimizations
Optimizations can make a big difference in the performance of your Go program. One way to optimize is by compiling your code with the C implementation of the main message loop, which can be done by passing the `walk_use_cgo` build tag.
This can be done using the `go build` command, followed by the `-tags` option and the `walk_use_cgo` tag. For example: `go build -tags walk_use_cgo`.
By using the C implementation, you can avoid the runtime overhead incurred by the usual default message loop, resulting in a more efficient program.
Consider reading: Gcloud Api Using Golang
PvsP

PvsP is a comparison between two packages, pwalk and pwalkdir. pwalk is a parallel implementation of filepath.Walk that speeds up file walking by utilizing goroutines.
The default behavior of pwalk is to use 2*runtime.NumCPU() goroutines for callbacks, which can be adjusted using the WalkN function. This allows for a flexible level of concurrency.
pwalkdir is actually faster than pwalk, but it requires at least Go 1.16. This makes it a more suitable choice for newer Go projects.
It's worth noting that pwalk is deprecated in favor of pwalkdir, so it's generally best to use the newer package unless you have a specific reason to stick with pwalk.
Discover more: Golang Use .env File
C Go Optimizations
Compiling your code with the right build tags can make a big difference in performance. You can use the walk_use_cgo build tag to compile Walk using a C implementation of the main message loop, which reduces runtime overhead.
This approach is especially useful if you're working with Go and need to minimize overhead from win32 API functions. By using the C implementation, you can avoid the extra runtime cost and get faster results.
The process is straightforward: simply use the go build command with the walk_use_cgo tag, like this: go build -tags walk_use_cgo.
Additional reading: Golang Use Cases
Constants and Parameters
In the world of Go, there are two special constants that help control the behavior of the `Walk` function: `SkipAll` and `SkipDir`. `SkipAll` is used to indicate that all remaining files and directories should be skipped, while `SkipDir` is used to skip a specific directory.
The `Walk` function takes two main parameters: `root` and `walkFn`. The `root` parameter is a string representing the root directory from which to start the walk, and `walkFn` is a function of type `WalkFunc` that's called for each file or directory in the tree.
Here are the parameters for the `WalkFunc` function, which is called for each file or directory:
- path: The full path of the file or directory being visited.
- info: An os.FileInfo object containing information about the file or directory.
- err: An error associated with accessing the file or directory.
Constants
Constants play a crucial role in coding, and understanding them can make a big difference in your workflow.
SkipAll is used as a return value from WalkFunc to indicate that all remaining files and directories are to be skipped.
In my experience, using constants like SkipAll can save you a lot of time and effort in the long run.
SkipDir is used as a return value from WalkFunc to indicate that the directory named in the call is to be skipped.
These constants are not returned as an error by any function, which means you can use them to control the flow of your code without worrying about errors.
Func Parameters
When working with functions that take parameters, it's essential to understand what those parameters are and how they're used. The Walk function, for example, has two parameters: root and walkFn.
The root parameter is a string that represents the root directory from which to start the walk. This is a crucial piece of information, as it determines the starting point for the function's operations.
The walkFn parameter is a function of type WalkFunc, which is called for each file or directory in the tree. This function takes three parameters: path, info, and err.
The path parameter is the full path of the file or directory being visited. The info parameter is an os.FileInfo object containing information about the file or directory. The err parameter is an error associated with accessing the file or directory.
A unique perspective: Golang Function Type
Here are the WalkFunc parameters in a concise table:
The function can handle or ignore this error, and the way it handles the error determines how the Walk function continues. If the function returns the special value SkipDir, Walk skips the current directory. If it returns SkipAll, Walk skips all remaining files and directories. Otherwise, if it returns a non-nil error, Walk stops entirely and returns that error.
Functionality
Go's walk function allows you to traverse a tree data structure in a depth-first manner. This means it visits a node and then moves to its children, exploring every path before backtracking.
The walk function in Go is particularly useful for tasks that require traversing a tree or graph data structure, such as finding all the nodes at a given depth or detecting cycles in the graph.
Func Dir
The Dir function is a useful tool in Go programming, and it's essential to understand how it works. Dir returns all but the last element of a path, which is typically the path's directory.

If the path is empty, Dir returns a single dot, which is the current directory. This is a clever feature that helps prevent errors when working with paths.
Dir also removes trailing slashes from the returned path, which makes it easier to work with paths in your code. For example, if you call Dir on the path "/home/user/", it will return "/home/user".
Here are some key facts to keep in mind when using Dir:
- Dir returns all but the last element of a path.
- Dir removes trailing slashes from the returned path.
- Dir returns a single dot if the path is empty.
- Dir returns a single separator if the path consists entirely of separators.
If you're working with paths in Go, the Dir function is an essential tool to have in your toolkit. It helps you navigate the file system and work with paths in a more efficient way.
Func EvalSymlinks
EvalSymlinks is a function that returns the path name after evaluating any symbolic links.
If the path is relative, the result will be relative to the current directory, unless one of the components is an absolute symbolic link.
EvalSymlinks calls Clean on the result, which means it cleans up the path name after evaluating the symbolic links.
Is Abs

Is Abs is a crucial functionality that helps determine the nature of a path.
The IsAbs function reports whether the path is absolute. This is a simple yet important distinction that can make a big difference in how your code behaves.
In the context of path manipulation, an absolute path refers to a path that starts from the root directory, whereas a relative path starts from the current working directory.
Basic Usage
The filepath.Walk function is a powerful tool for traversing directory trees. It starts from the root directory and recursively visits every file and directory.
You can use filepath.Walk to print the names of all files and directories within a directory tree, as demonstrated in the example. This is done by using the Walk function in conjunction with a callback function that prints the path of each file and directory.
Here's a simple example to illustrate this:
The callback function plays a crucial role in filepath.Walk, allowing you to customize the output and perform actions on each file and directory. This flexibility makes filepath.Walk a versatile tool for a variety of tasks.
Directory Size Calculation Example
Calculating directory size can be a useful task, especially when working with large files or directories. You can use the filepath.Walk function to sum the sizes of all files within a directory tree.
The calculateDirSize function is a great example of how to use filepath.Walk to traverse the directory tree. It's a simple yet effective way to get the job done.
To use filepath.Walk, you can call the function on the directory you want to calculate the size of. This will return a list of files and directories, which you can then use to sum the sizes of all files.
The total size of all files is then printed out, giving you a clear picture of the directory size. This is a handy feature to have when working with large files or directories.
Here's a step-by-step look at how the calculateDirSize function works:
N Deprecated
WalkN is a wrapper for filepath.Walk that can call multiple walkFns in parallel, allowing each item to be handled concurrently. A maximum of num walkFns will be called at any one time.
This approach can significantly speed up the walking process, especially for large directories. However, it's worth noting that WalkN is deprecated.
You can still use WalkN for now, but it's recommended to switch to the new WalkN function from github.com/opencontainers/selinux/pkg/pwalkdir, which is the replacement for the deprecated one.
Benchmarks and Comparison
The native Go implementation is actually the slowest when it comes to walking through directories, taking around 2x longer than the fastest implementation, godirwalk.
godirwalk is the clear winner in terms of speed, with the native implementation being generally fast enough for most cases but not the best option for high-performance scanning.
For a WalkFunc that does nothing but return, this implementation is about 10% slower than the standard library's filepath.Walk.
However, if the WalkFunc is doing some work, this implementation is usually faster, except when using WalkN with a single item, in which case it's slower.
Handling Errors
Handling errors is a crucial part of writing robust Go code. Errors can occur unexpectedly, and it's essential to handle them properly to prevent your program from crashing.
If an error occurs while accessing a path during a walk, it's logged, and the walk continues. This ensures that the walk doesn't stop due to a single error.
Broaden your view: Golang Create Error
By logging the error and continuing the walk, you can prevent a single error from bringing down your entire program. This approach is particularly useful when working with large datasets or complex systems.
Here are some key points to keep in mind when handling errors during a walk:
- Errors are logged and the walk continues.
Remember, handling errors is an essential part of writing reliable and maintainable code. By following these best practices, you can write code that's more robust and less prone to errors.
Take a look at this: Golang Code Comment Specifications
Examples
To create GUIs with Walk, you can use its declarative sub package. This is illustrated in a small example where you compile the manifest using the rsrc tool.
The filepath.Walk function is a powerful tool that starts from the root directory and recursively visits every file and directory. This is shown in an example where the callback function prints the path of each file and directory.
Here are the key points to remember about filepath.Walk:
- The filepath.Walk function starts from the root directory.
- The filepath.Walk function recursively visits every file and directory.
- The callback function can be used to print the path of each file and directory.
By using the declarative sub package and filepath.Walk function, you can efficiently create GUIs with Walk and traverse file systems with ease.
Syntax and Type
In Go, you'll often work with functions that take a Walker as an argument, like WalkFunc. The WalkFunc type is defined as a function that takes a Walker and returns nothing.
This type is crucial for working with the Walk interface, which is used for traversing file systems and other data structures.
A WalkFunc function is essentially a callback that gets called for each item in the Walk interface.
Intriguing read: Check Type of Interface Golang
Type: Func Parameters
The WalkFunc type is a function that's called by Walk to visit each file or directory, and it's defined as a type that takes three parameters: path, info, and err.
The path argument contains the root directory's argument as a prefix, which means if Walk is called with a root argument "dir" and finds a file named "a" in that directory, the walk function will be called with argument "dir/a". The directory and file are joined with Join, which may clean the directory name.
The info argument is the fs.FileInfo for the named path, and it contains information about the file or directory. The err argument reports an error related to the path, signaling that Walk will not walk into that directory.
Here are the WalkFunc parameters in a quick summary:
FilePath Syntax
FilePath syntax is used to specify the location of a file on a computer system.
In most operating systems, FilePath syntax starts with a root directory, which is typically denoted by a forward slash (/).
The root directory is the topmost directory in the file system hierarchy.
FilePaths can be absolute or relative, with absolute Paths starting from the root directory and relative Paths starting from the current working directory.
For example, the absolute Path /home/user/file.txt starts from the root directory, while the relative Path file.txt starts from the current working directory.
FilePaths can also include subdirectories, which are directories within other directories.
For instance, the Path /home/user/documents/file.txt includes two subdirectories: documents and user.
Featured Images: pexels.com


