go get github.com/daved/parth
Package parth provides path parsing for segment unmarshaling and slicing. In other words, parth provides simple and flexible access to (URL) path parameters.
Along with string, all basic non-alias types are supported. An interface is available for implementation by user-defined types. When handling an int, uint, or float of any size, the first valid value within the specified segment will be used.
VariablesfuncSegment(pathstring, iint, vinterface{}) errorfuncSequent(path, keystring, vinterface{}) errorfuncSpan(pathstring, i, jint) (string, error)
funcSubSeg(path, keystring, iint, vinterface{}) errorfuncSubSpan(path, keystring, i, jint) (string, error)
typeParthfuncNew(pathstring) *ParthfuncNewBySpan(pathstring, i, jint) *ParthfuncNewBySubSpan(path, keystring, i, jint) *Parthfunc (p*Parth) Err() errorfunc (p*Parth) Segment(iint, vinterface{})
func (p*Parth) Sequent(keystring, vinterface{})
func (p*Parth) Span(i, jint) stringfunc (p*Parth) SubSeg(keystring, iint, vinterface{})
func (p*Parth) SubSpan(keystring, i, jint) stringtypeUnmarshalerimport (
"fmt""github.com/daved/parth"
)
funchandler(w http.ResponseWriter, r*http.Request) {
varsstringiferr:=parth.Segment(r.URL.Path, 4, &s); err!=nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println(r.URL.Path)
fmt.Printf("%v (%T)\n", s, s)
// Output:// /some/path/things/42/others/3// others (string)
}import (
"fmt""github.com/daved/parth"
)
funchandler(w http.ResponseWriter, r*http.Request) {
variint64iferr:=parth.Sequent(r.URL.Path, "things", &i); err!=nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println(r.URL.Path)
fmt.Printf("%v (%T)\n", i, i)
// Output:// /some/path/things/42/others/3// 42 (int64)
}import (
"fmt""github.com/daved/parth"
)
funchandler(w http.ResponseWriter, r*http.Request) {
varsstringvarffloat32p:=parth.New(r.URL.Path)
p.Segment(2, &s)
p.SubSeg("key", 1, &f)
iferr:=p.Err(); err!=nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println(r.URL.Path)
fmt.Printf("%v (%T)\n", s, s)
fmt.Printf("%v (%T)\n", f, f)
// Output:// /zero/one/two/key/four/5.5/six// two (string)// 5.5 (float32)
}import (
"fmt""github.com/daved/parth"
)
funchandler(w http.ResponseWriter, r*http.Request) {
/* type mytype []byte func (m *mytype) UnmarshalSegment(seg string) error { *m = []byte(seg) } */varmmytypeiferr:=parth.Segment(r.URL.Path, 4, &m); err!=nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println(r.URL.Path)
fmt.Printf("%v == %q (%T)\n", m, m, m)
// Output:// /zero/one/two/key/four/5.5/six// [102 111 117 114] == "four" (mypkg.mytype)
}The most obvious use case for parth is when working with any URL path such as the one found at http.Request.URL.Path. parth is fast enough that it can be used multiple times in place of a single use of similar router-parameter schemes or even context.Context. There is no need to use an alternate http handler function definition in order to pass data that is already being passed. The http.Request type already holds URL data and parth is great at handling it. Additionally, parth takes care of parsing selected path segments into the types actually needed. Parth not only does more, it's usually faster and less intrusive than the alternatives.
If an index is negative, the negative count begins with the last segment. Providing a 0 for the second index is a special case which acts as an alias for the end of the path. An error is returned if: 1. Any index is out of range of the path; 2. When there are two indexes, the first index does not precede the second index.
If a key is involved, functions will only handle the portion of the path subsequent to the provided key. An error is returned if the key cannot be found in the path.
When handling an int, uint, or float of any size, the first valid value within the specified segment will be used.
View the GoDoc
Go 1.11
benchmark iter time/iter bytes alloc allocs
--------- ---- --------- ----------- ------
BenchmarkSegmentString-8 30000000 39.60 ns/op 0 B/op 0 allocs/op
BenchmarkSegmentInt-8 20000000 65.60 ns/op 0 B/op 0 allocs/op
BenchmarkSegmentIntNegIndex-8 20000000 86.60 ns/op 0 B/op 0 allocs/op
BenchmarkSpan-8 100000000 18.20 ns/op 0 B/op 0 allocs/op
BenchmarkStdlibSegmentString-8 5000000 454.00 ns/op 50 B/op 2 allocs/op
BenchmarkStdlibSegmentInt-8 3000000 526.00 ns/op 50 B/op 2 allocs/op
BenchmarkStdlibSpan-8 3000000 518.00 ns/op 69 B/op 2 allocs/op
BenchmarkContextLookupSetGet-8 1000000 1984.00 ns/op 480 B/op 6 allocs/op