Directories in Nim

Here’s the translation of the Go code to Nim, with explanations in Markdown format suitable for Hugo:

Our first program demonstrates how to work with directories in Nim. Here’s the full source code:

import os, strutils

proc check(e: bool) =
  if not e:
    raise newException(IOError, "An error occurred")

proc createEmptyFile(name: string) =
  writeFile(name, "")

proc main() =
  # Create a new sub-directory in the current working directory.
  createDir("subdir")
  check(dirExists("subdir"))

  # When creating temporary directories, it's good practice to remove them later.
  # We'll use a try-finally block to ensure cleanup.
  try:
    # Helper function to create a new empty file.
    createEmptyFile("subdir/file1")

    # We can create a hierarchy of directories, including parents.
    createDir("subdir/parent/child")
    check(dirExists("subdir/parent/child"))

    createEmptyFile("subdir/parent/file2")
    createEmptyFile("subdir/parent/file3")
    createEmptyFile("subdir/parent/child/file4")

    # List directory contents
    echo "Listing subdir/parent"
    for kind, name in walkDir("subdir/parent"):
      echo " ", name, " ", kind == pcDir

    # Change the current working directory
    setCurrentDir("subdir/parent/child")

    # Now we'll see the contents of subdir/parent/child when listing the current directory.
    echo "Listing subdir/parent/child"
    for kind, name in walkDir("."):
      echo " ", name, " ", kind == pcDir

    # Change back to where we started
    setCurrentDir("../../..")

    # We can also visit a directory recursively, including all its sub-directories.
    echo "Visiting subdir"
    for path in walkDirRec("subdir"):
      echo " ", path, " ", dirExists(path)

  finally:
    # Clean up by removing the directory and all its contents
    removeDir("subdir")

main()

To run the program, save it as directories.nim and use the Nim compiler:

$ nim c -r directories.nim
Listing subdir/parent
 subdir/parent/child true
 subdir/parent/file2 false
 subdir/parent/file3 false
Listing subdir/parent/child
 ./file4 false
Visiting subdir
 subdir/file1 false
 subdir/parent true
 subdir/parent/child true
 subdir/parent/child/file4 false
 subdir/parent/file2 false
 subdir/parent/file3 false

This Nim program demonstrates various operations on directories:

  1. Creating directories using createDir.
  2. Creating empty files with a helper function.
  3. Listing directory contents using walkDir.
  4. Changing the current working directory with setCurrentDir.
  5. Recursively walking a directory tree with walkDirRec.

The program uses Nim’s os module, which provides functions for interacting with the operating system, including file and directory operations.

Note that Nim uses exceptions for error handling, so we’ve defined a check procedure to raise an exception if an operation fails. We’ve also used a try-finally block to ensure that the temporary directory is cleaned up, even if an exception occurs.

The walkDir iterator is used to list directory contents, while walkDirRec is used for recursive directory traversal. These are analogous to the os.ReadDir and filepath.WalkDir functions in the original example.

Nim’s approach to file and directory operations is quite similar to Go’s, making the translation straightforward in most cases.