Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustParse ¶
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 ¶
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 ¶
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 ¶
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.