From ba4488a4a32590e9a1afca189e5d69b6a172236b Mon Sep 17 00:00:00 2001 From: kawashima Date: Wed, 1 Nov 2023 16:01:12 +0900 Subject: [PATCH] chore: add _example/main.go --- _example/main.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 _example/main.go diff --git a/_example/main.go b/_example/main.go new file mode 100644 index 0000000..2cc235b --- /dev/null +++ b/_example/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "log" + "regexp" + "strings" + + "github.com/nekrassov01/access-log-parser" +) + +var ( + fields = []string{"field1", "field2", "field3"} + patterns = []*regexp.Regexp{regexp.MustCompile("([!-~]+) ([!-~]+) ([!-~]+)")} + sample = `aaa bbb ccc +xxx yyy zzz +111 222 333` +) + +func main() { + p := parser.New(fields, patterns) + out, err := p.Parse(strings.NewReader(sample), nil) + if err != nil { + log.Fatal(err) + } + fmt.Println(strings.Join(out.Data, "\n")) + fmt.Println(out.Metadata) +} + +/* +{"index":1,"field1":"aaa","field2":"bbb","field3":"ccc"} +{"index":2,"field1":"xxx","field2":"yyy","field3":"zzz"} +{"index":3,"field1":"111","field2":"222","field3":"333"} +{"total":3,"matched":3,"unmatched":0,"skipped":0,"source":"","errors":null} +*/