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!
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;
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
PathandPurePathare interfaces, properties inpathlibare now methods ingo-pathlib, e.g.Path.namebecomesPath.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)andPath.RenameToString(target String)Path.replace()->Path.ReplaceWithPath(target Path)andPath.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()
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 |