Documentation ¶
Overview ¶
Package nmfmt wraps fmt.Xprintf functions providing $name style placeholders.
Each nmfmt.Xprintf function has a signature like:
func Printf(format string, m map[string]any) (int, error)
And prints according to a format that contains $name style placeholders.
Placeholders ¶
Example: `variable1:q`
Each placeholder in the format is like $name, ${name}, $name:verb or ${name:verb}. The names are keys of the map m. (case sensitive) And their values are to be embedded.
Name ¶
Must match \w.
See [Named], Struct
Verb ¶
Verb is with `:`. Must match \w.
Defaults to `v`.
debug notation ¶
If a placeholder starts with `$=`, the output starts with the name of the placeholder followed by `=`.
`$=name` -> `name=NAME_VALUE`
Example ¶
package main import ( "github.com/shu-go/nmfmt" ) func main() { nmfmt.Printf("$name is $age years old.\n", "name", "Kim", "age", 22) nmfmt.Printf("$name ${ name } $name:q ${name:q}aaa\n", "name", "Kim", "age", 22) }
Output: Kim is 22 years old. Kim Kim "Kim" "Kim"aaa
Example (Debug) ¶
package main import ( "github.com/shu-go/nmfmt" ) func main() { name := "Kim" greeting := "Hello" nmfmt.Printf("$=greeting:q, $=name\n", nmfmt.M{"greeting": greeting, "name": name}) }
Output: greeting="Hello", name=Kim
Example (Map) ¶
package main import ( "github.com/shu-go/nmfmt" ) func main() { name := "Kim" age := 22 nmfmt.Printf("$name is $age years old.\n", nmfmt.M{"name": name, "age": age}) nmfmt.Printf("$name ${ name } $name:q ${name:q}aaa\n", nmfmt.M{"name": name}) }
Output: Kim is 22 years old. Kim Kim "Kim" "Kim"aaa
Index ¶
- func Errorf(format string, a ...any) error
- func ExtractNames(format string) map[string]struct{}
- func Fprintf(w io.Writer, format string, a ...any) (int, error)
- func Printf(format string, a ...any) (int, error)
- func Sprintf(format string, a ...any) string
- func Struct(structs ...any) []any
- type Formatter
- type M
- type OptionFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractNames ¶ added in v0.2.0
func Struct ¶
Struct returns a slice of names and values of fields from structs.
If a key is duplicated among structs, the first found element wins.
Note: An unexported field results in <nil>. (NO: name string; YES: Name string)
Example ¶
package main import ( "github.com/shu-go/nmfmt" ) func main() { nmfmt.Printf("$Name is $Age years old.\n", nmfmt.Struct(struct { Name string Age int }{Name: "Kim", Age: 22})...) }
Output: Kim is 22 years old.
Types ¶
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
func New ¶
func New(opts ...OptionFunc) Formatter
type OptionFunc ¶
type OptionFunc func(*formatterOptions)
func CacheResetLimit ¶
func CacheResetLimit(misses int) OptionFunc
CacheResetLimit sets when to clear cache.