GitHub - jonchun/pathtype: Add a type for paths in Go. (original) (raw)
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.
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()) } } }