Documentation
¶
Overview ¶
Package hasty provides functions that are useful for quick'n'dirty coding (small "script" programs, puzzle solving, etc).
Not for use in production code.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoMatch = errors.New("hasty: provided regular expression did not match data")
ErrNoMatch is returned by Parse if the regular expression did not match the
Functions ¶
func MustParse ¶
MustParse is like Parse except that it panics instead of returning a non-nil error.
func Parse ¶
Parse uses r to parse data and loads it into v.
The target v must be a pointer to a value of struct type. The exported struct fields correspond to named capture groups of r. The following types are supported:
- If the field implements encoding.TextUnmarshaler, then that is used.
- If the field is a string or a []byte, the matching string is copied as-is.
- If the field is any integer type, the captured string is parsed as a decimal integer.
If the struct value contains any other exported fields, Parse returns a non-nil error.
If r contains any named capture groups which don't correspond to a field in v, Parse returns a non-nil error. Unnamed capture groups are ignored.
Example ¶
package main
import (
"fmt"
"log"
"regexp"
"github.com/cespare/hasty"
)
func main() {
var person struct {
Name string
Age int
}
data := `{Alice Smith, 44}`
re := regexp.MustCompile(`^{(?P<Name>[\w ]+),\s+(?P<Age>\d+)}$`)
if err := hasty.Parse([]byte(data), &person, re); err != nil {
log.Fatal(err)
}
fmt.Printf("%s (%d)\n", person.Name, person.Age)
}
Output: Alice Smith (44)
Types ¶
This section is empty.