Directories in Dart

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

Dart provides several useful functions for working with directories in the file system.

import 'dart:io';

void check(FileSystemException? e) {
  if (e != null) {
    throw e;
  }
}

void main() {
  // Create a new sub-directory in the current working directory.
  Directory('subdir').createSync();

  // When creating temporary directories, it's good practice to delete them
  // when done. We can use a try-finally block for this purpose.
  try {
    // Helper function to create a new empty file.
    void createEmptyFile(String name) {
      File(name).writeAsStringSync('');
    }

    createEmptyFile('subdir/file1');

    // We can create a hierarchy of directories, including parents.
    // This is similar to the command-line `mkdir -p`.
    Directory('subdir/parent/child').createSync(recursive: true);

    createEmptyFile('subdir/parent/file2');
    createEmptyFile('subdir/parent/file3');
    createEmptyFile('subdir/parent/child/file4');

    // List directory contents.
    print('Listing subdir/parent');
    Directory('subdir/parent').listSync().forEach((entity) {
      print(' ${entity.path} ${entity is Directory}');
    });

    // Change the current working directory.
    Directory.current = Directory('subdir/parent/child');

    // Now we'll see the contents of `subdir/parent/child` when listing
    // the current directory.
    print('Listing subdir/parent/child');
    Directory.current.listSync().forEach((entity) {
      print(' ${entity.path} ${entity is Directory}');
    });

    // Change back to where we started.
    Directory.current = Directory('../../..');

    // We can also visit a directory recursively, including all its
    // sub-directories.
    print('Visiting subdir');
    _visitRecursively(Directory('subdir'));
  } finally {
    // Clean up by deleting the created directory and all its contents.
    Directory('subdir').deleteSync(recursive: true);
  }
}

void _visitRecursively(Directory dir) {
  dir.listSync(recursive: true, followLinks: false).forEach((entity) {
    print(' ${entity.path} ${entity is Directory}');
  });
}

To run the program, save it as directories.dart and use dart run:

$ dart run directories.dart
Listing subdir/parent
 subdir/parent/child true
 subdir/parent/file2 false
 subdir/parent/file3 false
Listing subdir/parent/child
 subdir/parent/child/file4 false
Visiting subdir
 subdir true
 subdir/file1 false
 subdir/parent true
 subdir/parent/child true
 subdir/parent/child/file4 false
 subdir/parent/file2 false
 subdir/parent/file3 false

This Dart code demonstrates various operations on directories, including creating directories, listing contents, changing the current working directory, and recursively visiting directories. It uses the dart:io library, which provides functionality for working with files and directories.

Note that Dart’s approach to file system operations is slightly different from Go’s. For example, Dart uses synchronous methods (with Sync suffix) for simplicity in this example, but asynchronous versions are available and generally preferred for better performance in real applications. Also, Dart doesn’t have a direct equivalent to Go’s filepath.WalkDir, so we implement a simple recursive directory traversal function instead.