Skip to content

rewgs/go-pathlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

105 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-pathlib

A lot of programmers move from Python to Go (myself included). One of Python's best features is the excellent pathlib module, and personally I've found it difficult to give up. Thus, go-pathlib: an implementation of pathlib for Go, intended to make Pythonistas-turned-Gophers feel a little more at home.

Note

This package is in extremely early development.

Contributions are welcome!

Similarities to pathlib

Like Python's pathlib, go-pathlib contains two primary types: Path and PurePath.

PurePath is the base type upon which all else rests. PurePath instances provide purely computational operations without I/O.

Path types, also known as "concrete paths," contain embedded PurePath structs and provide I/O operations.

Additionally, both PurePath and Path contain OS-specific implementations: PurePosixPath, PureWindowsPath, PosixPath, and WindowsPath.

Just like Python's pathlib, their relationships can be diagrammed as follows:

graph BT;
    PurePosixPath --> PurePath;
    Path --> PurePath;
    PureWindowsPath --> PurePath;
    PosixPath --> PurePosixPath;
    PosixPath --> Path;
    WindowsPath --> PureWindowsPath;
    WindowsPath --> Path;
Loading

Deviations from pathlib

In contrast to chigopher's Go package pathlib, which states "it takes many cues from Python's pathlib, although it does not strictly adhere to its design philosophy," go-pathlib does -- an effort has been made to keep go-pathlib as similar to pathlib as possible, even when that results in some minor code smells such as embedded structs.

However, some deviations from pathlib proved to be inevitable:

  • Obviously, capitalizations must change due to Go's export system.
  • As Path and PurePath are interfaces, properties in pathlib are now methods in go-pathlib, e.g. Path.name becomes Path.Name().
  • Because Go does not support generic type parameters for methods, the following methods have been split into two:
    • Path.rename() -> Path.RenameToPath(target Path) and Path.RenameToString(target String)
    • Path.replace() -> Path.ReplaceWithPath(target Path) and Path.ReplaceWithString(target string)
    • Path.hardlink_to() -> Path.HardlinkToPath(target Path) Path.HardlinkToString(target string)

Additionally, some quality-of-life improvements not present in pathlib have been added:

  • Path.AsString()
  • path.NewFromPurePath()

Parity with pathlib

Path

pathlib go-pathlib status test coverage
Path.absolute() Path.Absolute() todo todo
Path.chmod() Path.Chmod() todo todo
Path.cwd() path.Cwd() finished todo
Path.exists() Path.Exists() finished in progress
Path.expanduser() Path.ExpandUser() todo todo
Path.hardlink_to() Path.HardlinkToPath() todo todo
Path.HardlinkToString() todo todo
Path.is_dir() Path.IsDir() todo todo
Path.is_file() Path.IsFile() todo todo
Path.is_junction() Path.IsJunction() todo todo
Path.is_mount() Path.IsMount() todo todo
Path.is_symlink() Path.IsSymlink() todo
Path.iterdir() Path.Iterdir() todo
Path.lchmod() Path.Lchmod() todo
Path.lstat() Path.Lstat() todo
Path.mkdir() Path.Mkdir() todo
Path.resolve() Path.Resolve() todo
Path.readlink() Path.Readlink() todo
Path.rename() Path.Rename() todo
Path.replace() Path.Replace() todo
Path.samefile() Path.Samefile() todo
Path.stat() Path.Stat() todo
Path.symlink_to() Path.SymlinkTo() todo
Path.unlink() Path.Unlink() todo
Path.rmdir() Path.Rmdir() todo
Path.walk() Path.Walk() todo
Path.home() path.Home() finished todo

PurePath

pathlib go-pathlib status test coverage
PurePath.anchor PurePosixPath.Anchor() finished finished
PureWindowsPath.Anchor() finished finished
PurePath.drive PurePosixPath.Drive() finished todo
PureWindowsPath.Drive() finished todo

About

So that Pythonistas-turned-Gophers can feel a little more at home.

Resources

License

Contributing

Stars

Watchers

Forks

Contributors