Here’s the translation of the Go code to Swift, with explanations in Markdown format suitable for Hugo:
The Foundation framework provides functions to parse and construct file paths in a way that is portable between operating systems; dir/file on Linux vs. dir\file on Windows, for example.
To run the program, save it as FilePaths.swift and use swift command:
Note that Swift’s file path handling is somewhat different from Go’s. We’re using NSString methods here because they provide similar functionality to Go’s filepath package. The Foundation framework in Swift doesn’t have a direct equivalent to Go’s filepath.Rel, so we’ve used relativePath(from:) which is similar but returns nil instead of an error when the path can’t be made relative.