Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jonchun/pathtype
Add a type for paths in Go.
https://github.com/jonchun/pathtype
file filepath files filesystem go golang path
Last synced: about 2 months ago
JSON representation
Add a type for paths in Go.
- Host: GitHub
- URL: https://github.com/jonchun/pathtype
- Owner: jonchun
- License: mit
- Created: 2021-08-03T09:59:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-12T15:10:37.000Z (over 3 years ago)
- Last Synced: 2024-07-31T20:51:28.286Z (5 months ago)
- Topics: file, filepath, files, filesystem, go, golang, path
- Language: Go
- Homepage: https://godoc.org/github.com/jonchun/pathtype
- Size: 38.1 KB
- Stars: 13
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - pathtype - Treat paths as their own type instead of using strings. (File Handling / Search and Analytic Databases)
- awesome-go-extra - pathtype - 08-03T09:59:44Z|2021-08-12T15:10:37Z| (File Handling / Advanced Console UIs)
README
# pathtype
Treat paths as their own type instead of using strings. This small package wraps functions from the standard library to create a new Path type.
## Example
### Code
```go
package mainimport (
"fmt"
"log"pt "github.com/jonchun/pathtype"
)type path = pt.Path
func main() {
myFile := path("myfile.txt")
exampleFile := path("example/example.txt")
fmt.Println(exampleFile.Dir())
fmt.Println(exampleFile.Dir().Join(myFile))res, err := exampleFile.Dir().Join(myFile).Dir().Abs()
if err != nil {
log.Fatal(err)
}
fmt.Println(res)fmt.Println("=========================")
listBase(res)
fmt.Println("=========================")
listExt(res)
}// list all Base for files in p
func listBase(p path) {
if glob, err := p.Glob("*"); err != nil {
log.Fatal(err)
} else {
for _, match := range glob {
fmt.Println(match.Base())
}
}
}// list all extensions for files in p
func listExt(p path) {
if glob, err := p.Glob("*"); err != nil {
log.Fatal(err)
} else {
for _, match := range glob {
fmt.Println(match.Ext())
}
}
}
```### Output
```
example
example/myfile.txt
/home/jonchun/example_module/example
=========================
1.log
2.log
example.txt
=========================
.log
.log
.txt
```See [GoDoc](https://godoc.org/github.com/jonchun/pathtype) for documentation, but it should be pretty self-explanatory.
## TODO
- Update examples for all the methods and have 1-to-1 coverage of the official examples/docs.