Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

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 main

import (
"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.