Documentation
¶
Overview ¶
Package formatter implements “replacement fields” surrounded by curly braces {} format strings.
Index ¶
- Constants
- func Format(message string, arguments ...interface{}) (string, error)
- func FormatWriter(writer io.Writer, message string, arguments ...interface{}) error
- func MustFormat(message string, arguments ...interface{}) string
- type Formatter
- func (f *Formatter) AddFunction(name string, function interface{}) *Formatter
- func (f *Formatter) AddFunctions(functions Functions) *Formatter
- func (f *Formatter) Format(message string, arguments ...interface{}) (string, error)
- func (f *Formatter) FormatWriter(writer io.Writer, message string, arguments ...interface{}) error
- func (f *Formatter) GetDelimiters() (left, right string)
- func (f *Formatter) GetFunction(name string) interface{}
- func (f *Formatter) GetFunctions() Functions
- func (f *Formatter) GetLeftDelimiter() string
- func (f *Formatter) GetPlaceholder() string
- func (f *Formatter) GetRightDelimiter() string
- func (f *Formatter) MustFormat(message string, arguments ...interface{}) string
- func (f *Formatter) RemoveFunction(name string) *Formatter
- func (f *Formatter) RemoveFunctions(names []string) *Formatter
- func (f *Formatter) Reset() *Formatter
- func (f *Formatter) ResetDelimiters() *Formatter
- func (f *Formatter) ResetFunctions() *Formatter
- func (f *Formatter) ResetLeftDelimiter() *Formatter
- func (f *Formatter) ResetPlaceholder() *Formatter
- func (f *Formatter) ResetRightDelimiter() *Formatter
- func (f *Formatter) SetDelimiters(left, right string) *Formatter
- func (f *Formatter) SetFunctions(functions Functions) *Formatter
- func (f *Formatter) SetLeftDelimiter(delimiter string) *Formatter
- func (f *Formatter) SetPlaceholder(placeholder string) *Formatter
- func (f *Formatter) SetRightDelimiter(delimiter string) *Formatter
- type Functions
- type Named
Examples ¶
- Format (AutomaticPlaceholder)
- Format (BackgroundBrightColors)
- Format (BackgroundColors)
- Format (BackgroundRGB)
- Format (BrightColors)
- Format (Colors)
- Format (MixedPlaceholders)
- Format (NamedPlaceholders)
- Format (ObjectAutomaticPlaceholders)
- Format (ObjectPlaceholders)
- Format (ObjectPointerPlaceholders)
- Format (ObjectPositionalPlaceholders)
- Format (PositionalPlaceholders)
- Format (Rgb)
- Format (SetDelimiters)
- Format (SetFunctions)
- Format (SetPlaceholder)
- Format (WithArguments)
- Format (WithoutArguments)
- FormatWriter
- MustFormat
Constants ¶
const ( DefaultPlaceholder = "p" DefaultLeftDelimiter = "{" DefaultRightDelimiter = "}" )
These constants define default values used by formatter.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format formats string.
Example (AutomaticPlaceholder) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("Automatic placeholder {p}:{p}:{p}():", "dir/file", 1, "func1")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Automatic placeholder dir/file:1:func1():
Example (BackgroundBrightColors) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("With background bright colors {Cyan | Bright | Background}cyan{Normal}")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Example (BackgroundColors) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("With background colors {Yellow | Background}yellow{Normal}")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Example (BackgroundRGB) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("With background RGB {RGB 255 165 0 | Background}funky{Normal}")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Example (BrightColors) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("With bright colors {Magenta | Bright}magenta{Normal}")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Example (Colors) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("With colors {Red}red{Normal} {Green}green{Normal} {Blue}blue{Normal}")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Example (MixedPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
objectPointer := &struct {
X int
Y int
Z int
}{
X: 2,
Z: 6,
Y: 3,
}
formatted, err := formatter.Format("Mixed placeholders {.X}.{p}.{.Y}.{.Z} {p1} {p0}", objectPointer, "b", "c", nil)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Mixed placeholders 2.{2 3 6}.3.6 b {2 3 6} c <nil>
Example (NamedPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("Named placeholders {file}:{line}:{function}():", formatter.Named{
"line": 3,
"function": "func1",
"file": "dir/file",
})
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Named placeholders dir/file:3:func1():
Example (ObjectAutomaticPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
object1 := struct {
X int
}{
X: 1,
}
object2 := struct {
Message string
}{
Message: "msg",
}
formatted, err := formatter.Format("{p.X} {p.Message}", object1, object2)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: 1 msg
Example (ObjectPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
object := struct {
Line int
Function string
File string
}{
Line: 4,
Function: "func1",
File: "dir/file",
}
formatted, err := formatter.Format("Object placeholders {.File}:{.Line}:{.Function}():", object)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Object placeholders dir/file:4:func1():
Example (ObjectPointerPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
objectPointer := &struct {
X int
Y int
Z int
}{
X: 4,
Z: 3,
Y: 1,
}
formatted, err := formatter.Format("Object placeholders {.X}.{.Y}.{.Z}", objectPointer)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Object placeholders 4.1.3
Example (ObjectPositionalPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
object1 := struct {
X int
}{
X: 1,
}
object2 := struct {
Y int
}{
Y: 2,
}
formatted, err := formatter.Format("{p1.Y} {p0.X}", object1, object2)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: 2 1
Example (PositionalPlaceholders) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("Positional placeholders {p1}:{p0}:{p2}():", 2, "dir/file", "func1")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Positional placeholders dir/file:2:func1():
Example (Rgb) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("With RGB {RGB 255 165 0}funky{Normal}")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Example (SetDelimiters) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.New().SetDelimiters("<", ">").Format("Custom delimiters <p1> <p0>", "4", 3)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Custom delimiters 3 4
Example (SetFunctions) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
functions := formatter.Functions{
"str": func() string {
return "text"
},
"number": func() int {
return 3
},
"boolean": func() bool {
return true
},
"floating": func() float64 {
return 4.5
},
}
formatted, err := formatter.New().SetFunctions(functions).Format("Custom functions {str} {p} {number} {boolean} {floating}", 5)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Custom functions text 5 3 true 4.5
Example (SetPlaceholder) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.New().SetPlaceholder("arg").Format("Custom placeholder {arg1} {arg0}", "2", 3)
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Custom placeholder 3 2
Example (WithArguments) ¶
formatted, err := formatter.Format("With arguments", 3, nil, 4.5, true, "arg1", []byte{}, Error("error"))
if err != nil {
panic(err)
}
fmt.Println(formatted)
Output: With arguments 3 <nil> 4.5 true arg1 [] error
Example (WithoutArguments) ¶
package main
import (
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
formatted, err := formatter.Format("Without arguments")
if err != nil {
panic(err)
}
fmt.Println(formatted)
}
Output: Without arguments
func FormatWriter ¶
FormatWriter formats string to writer.
Example ¶
package main
import (
"bytes"
"fmt"
"gitlab.com/tymonx/go-formatter/formatter"
)
func main() {
buffer := new(bytes.Buffer)
if err := formatter.FormatWriter(buffer, "Writer {p2}", 3, "foo", "bar"); err != nil {
panic(err)
}
fmt.Println(buffer.String())
}
Output: Writer bar 3 foo
func MustFormat ¶ added in v1.1.0
MustFormat is like Format but panics if provided message cannot be formatted. It simplifies safe initialization of global variables holding formatted strings.
Example ¶
fmt.Println(formatter.MustFormat("With arguments", 3, nil, false, 4.5, "text", []byte{}, Error("error")))
Output: With arguments 3 <nil> false 4.5 text [] error
Types ¶
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter defines a formatter object that formats string using “replacement fields” surrounded by curly braces {}.
func (*Formatter) AddFunction ¶
AddFunction adds template function used by formatter.
func (*Formatter) AddFunctions ¶
AddFunctions adds template functions used by formatter.
func (*Formatter) FormatWriter ¶
FormatWriter formats string to writer.
func (*Formatter) GetDelimiters ¶
GetDelimiters returns delimiters used by formatter. Default is {}.
func (*Formatter) GetFunction ¶
GetFunction returns template function used by formatter.
func (*Formatter) GetFunctions ¶
GetFunctions returns template functions used by formatter.
func (*Formatter) GetLeftDelimiter ¶
GetLeftDelimiter returns left delimiter used by formatter. Default is {.
func (*Formatter) GetPlaceholder ¶
GetPlaceholder returns placeholder string prefix used for automatic and positional placeholders to format string. Default is p.
func (*Formatter) GetRightDelimiter ¶
GetRightDelimiter returns right delimiter used by formatter. Default is }.
func (*Formatter) MustFormat ¶ added in v1.1.0
MustFormat is like Format but panics if provided message cannot be formatted. It simplifies safe initialization of global variables holding formatted strings.
func (*Formatter) RemoveFunction ¶
RemoveFunction removes template function used by formatter.
func (*Formatter) RemoveFunctions ¶
RemoveFunctions removes template functions used by formatter.
func (*Formatter) ResetDelimiters ¶
ResetDelimiters resets delimiters used by formatter to default values.
func (*Formatter) ResetFunctions ¶
ResetFunctions resets template functions used by formatter.
func (*Formatter) ResetLeftDelimiter ¶
ResetLeftDelimiter resets left delimiter used by formatter to default value.
func (*Formatter) ResetPlaceholder ¶
ResetPlaceholder resets placeholder to default value.
func (*Formatter) ResetRightDelimiter ¶
ResetRightDelimiter resets right delimiter used by formatter to default value.
func (*Formatter) SetDelimiters ¶
SetDelimiters sets delimiters used by formatter. Default is {}.
func (*Formatter) SetFunctions ¶
SetFunctions sets template functions used by formatter.
func (*Formatter) SetLeftDelimiter ¶
SetLeftDelimiter sets left delimiter used by formatter. Default is {.
func (*Formatter) SetPlaceholder ¶
SetPlaceholder sets placeholder string prefix used for automatic and positional placeholders to format string. Default is p.
func (*Formatter) SetRightDelimiter ¶
SetRightDelimiter sets right delimiter used by formatter. Default is }.