strparse

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

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 4 Imported by: 1

README

go-strparse

Package strparse provides tools for parsing strings and returning basic Go types, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-strparse

GoDoc

Examples

Here is an example of trying to parse a string and returning a uint16:

tcpport, error := strparse.Parse[uint16]("1234")

Here is an example of trying to parse a string and returning a uint16, but if the string doesn't represent a uint16 number, then defaulting it to 8080:

tcpport := strparse.ParseElse[uint16]("1234", 8080)

Import

To import package strparse use import code like the following:

import "github.com/reiver/go-strparse"

Installation

To install package strparse do the following:

GOPROXY=direct go get github.com/reiver/go-strparse

Author

Package strparse was written by Charles Iliya Krempeaux

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustParse

func MustParse[T Type](str string) T

MustParse parses a string, and tries to return the specified type. If the string is not parsable to the speciied type, the MustParse panic()s.

For example:

tcpPort := strparse.MustParse[uint16]("1234")

Or, for example:

duration := strparse.MustParse[time.Duration]("5m10s")

Also, for example:

number := strparse.MustParse[int64]("--NOT-A-NUMBER--")

Note that this example will panic(), because the string "--NOT-A-NUMBER--" is not parsable to an int64.

See Type for a list of permissible types.

func Parse

func Parse[T Type](value string) (T, error)

Parse parses a string, and tries to return the specified type. If the parsing fails, Parse returns an error.

For example:

tcpPort, err := strparse.Parse[uint16]("1234")

In this example, the string "1234" is parsed as a uint16.

For another example:

duration, err := strparse.Parse[time.Duration]("5m10s")

In this example, the string "5m10s" is parsed as a time.Duration.

See Type for a list of permissible types.

func ParseElse

func ParseElse[T Type](str string, alt T) T

ParseElse parses a string, and tries to return the specified type. If the string is not parsable to the speciied type, the ParseElse returns the `alt` value.

For example:

tcpPort := strparse.ParseElse[uint16]("1234", 8080)

In this example, the string "1234" is parsed as a uint16.

Or, for example:

duration := strparse.Parse[time.Duration]("5m10s", 2 * time.Minute)

In this example, the string "5m10s" is parsed as a time.Duration.

For yet example:

number := strparse.ParseElse[int64]("--NOT-A-NUMBER--", 7)

In this example, it tries to parse the string "--NOT-A-NUMBER--" is an int64, but fail, and returns the alterantive value of 7.

Note that the 2nd return parameter in all of these examples is the alternative value.

See Type for a list of permissible types.

func ParseOK

func ParseOK[T Type](str string) (T, bool)

ParseOK is similar to Parse except that it return a bool rather than an error if there is an error.

For example:

tcpPort, ok := strparse.ParseOK[uint16]("1234")

In this example, the string "1234" is parsed as a uint16.

For another example:

duration, ok := strparse.ParseOK[time.Duration]("5m10s")

In this example, the string "5m10s" is parsed as a time.Duration.

See Type for a list of permissible types.

Types

type Type

type Type interface {
	time.Duration |
		string |
		bool |
		int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64
}

Type specifies the types that Parse, ParseElse, and MustParse are able to return.

Jump to

Keyboard shortcuts

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