str

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2015 License: MIT Imports: 1 Imported by: 0

README

str GoDoc

String parsing package for Go. Converts strings to best guess value type.

Supported types (in this order):

  • nil or null
  • bool
  • int
  • int64
  • uint
  • uint64
  • float64

Quoted values are always strings with the quotes stripped.

For examples see the test code.

Get started

go get gopkg.in/matryer/str.v1

Usage

The simplest usage:

val := str.Parse("123")

To add additional ParseFunc functions:

parser := append(str.DefaultParser, toMyRangeType)
val := parser.Parse("[1,3]")

To use your own parsers entirely, use New:

parser := str.New(toMyRangeType)
val := parser.Parse("[1,3]")
Time

To add parsers for time, import the parsers package and use the Time method:

parser := append(str.DefaultParser, parsers.Time(time.RFC822))
val := parser.Parse("02 Jan 06 15:04 MST")

Documentation

Overview

Package str provides string parsing capabilities for Go.

Simplest usage

val := str.Parse(s)

Index

Constants

This section is empty.

Variables

DefaultParser is the default Parser that includes all built-in ParseFunc functions.

Functions

func Parse

func Parse(s string) interface{}

Parse parses s with the DefaultParser. For more information, see Parser.Parse.

Types

type ParseFunc

type ParseFunc func(s string) (interface{}, bool)

ParseFunc is a function that attempts to parse the string into a real type and returns whether that was successful or not.

type Parser

type Parser []ParseFunc

Parser is a slice of ParseFunc functions that will be called in order.

func New

func New(funcs ...ParseFunc) Parser

New creates a new Parser with the specified ParseFunc functions. Each function will be tried in order until one is able to parse the strings. If none can, the original string is returned untouched.

func (Parser) Parse

func (p Parser) Parse(s string) interface{}

Parse parses s with each ParseFunc in order returning the result. If no parsers are successful, returns the string untouched.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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