Documentation ¶
Overview ¶
Package strings offers an extension of functionality to the default golang strings package.
Index ¶
- type BaseCaster
- type Converter
- func (c *Converter) Convert(value interface{}) string
- func (c *Converter) Localised() *Converter
- func (c *Converter) NotPaddingZeros() *Converter
- func (c *Converter) PaddingZeros() *Converter
- func (c *Converter) QuotingStrings() *Converter
- func (c *Converter) WithFloatingPointPrecision(precision int) *Converter
- type FluentBuilder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseCaster ¶
type BaseCaster struct {
// contains filtered or unexported fields
}
BaseCaster allows strings to be cast into other base types where possible. With a fluent interface, it allows the caller fine-tuned control over how strings are cast to other base-types.
func (*BaseCaster) Cast ¶
func (bs *BaseCaster) Cast(valueAsString string) interface{}
Cast attempts to convert the value supplied as a string to another Golang base type, depending on how the BaseCaster has been configured. If it cannot, the value is returned with its original string type.
Example ¶
caster := new(BaseCaster).WithNumbersAsFloats() fortyTwoAsString := "42" sevenAsString := "7" fortyTwo, fortyTwoIsFloat := caster.Cast(fortyTwoAsString).(float64) seven, sevenIsFloat := caster.Cast(sevenAsString).(float64) var actualResult float64 if sevenIsFloat && fortyTwoIsFloat { actualResult = fortyTwo / seven } fmt.Printf("%v", actualResult)
Output: 6
func (*BaseCaster) WithNumbersAsFloats ¶
func (bs *BaseCaster) WithNumbersAsFloats() *BaseCaster
WithNumbersAsFloats attempts to convert all numbers to floating point number base type.
type Converter ¶
type Converter struct {
// contains filtered or unexported fields
}
func NewConverter ¶
func NewConverter() *Converter
func (*Converter) NotPaddingZeros ¶
func (*Converter) PaddingZeros ¶
func (*Converter) QuotingStrings ¶
func (*Converter) WithFloatingPointPrecision ¶
type FluentBuilder ¶
FluentBuilder offers a 'fluent' interface (see https://en.wikipedia.org/wiki/Fluent_interface) to the golang strings.Builder, allowing us to chain .Add() calls
func (*FluentBuilder) Add ¶
func (fb *FluentBuilder) Add(strings ...string) *FluentBuilder
Add appends the contents of each supplied strings to its buffer, returning a reference to the FluentBuilder, allowing a chaining of a number of Add() calls
Example ¶
PetsAndFoods := new(FluentBuilder).Add("cat", "-dog-", "canary, ").Add("pie", "-cake-", "tart").String() fmt.Print(PetsAndFoods)
Output: cat-dog-canary, pie-cake-tart
func (*FluentBuilder) AddIf ¶
func (fb *FluentBuilder) AddIf(shouldAdd bool, strings ...string) *FluentBuilder
AddIf appends the contents of each supplied strings to its buffer, returning a reference to the FluentBuilder, only when shouldAdd is true, allowing a chaining of a number of Add() calls