restruct

package module
v0.0.0-...-be47d98 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 5 Imported by: 0

README

restruct

Parse regular expressions to structs.

Go Reference

Supported conversions

Matches can be parsed into the following field types:

Example

type Foo struct {
    Bar string
    Baz float64
}

parse, _ := Compile[Foo](`(?<Bar>[a-z]+) (?<Baz>[0-9.]+)`)

foo, _ := parse(`yadda 3.14`)

fmt.Printf("%s/%.3f", foo.Bar, foo.Baz) // Output: yadda/3.140

Documentation

Overview

Example
parse := Compile[struct {
	RawMatch
	Position
	Foo int
	Bar time.Time
}](`(?P<Foo>[0-9]+)/(?P<Bar>[^\s]+)`)

foo, err := parse("---> 42/2022-02-22T09:00:00Z <---")
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(foo.Foo)
fmt.Println(foo.Bar)
fmt.Println(foo)
fmt.Println(foo.Position)
Output:

42
2022-02-22 09:00:00 +0000 UTC
42/2022-02-22T09:00:00Z
5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile[T any](restr string) func(s string) (T, error)

Types

type Position

type Position int

type RawMatch

type RawMatch string

func (RawMatch) String

func (r RawMatch) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL