Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

/p/nt/ufmt/v0

Directory · 5 Files
README.md Open

v0 - Unaudited This is an initial version of this package that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.

ufmt - String formatting

Gno port of a subset of Go's fmt package (micro-fmt). Provides Printf, Sprintf, Errorf and friends for formatting strings with verb-based templates.

Usage

 1import "gno.land/p/nt/ufmt/v0"
 2
 3s := ufmt.Sprintf("hello %s, you are %d years old", "alice", 30)
 4// "hello alice, you are 30 years old"
 5
 6err := ufmt.Errorf("invalid id: %q", input)
 7
 8var buf bytes.Buffer
 9ufmt.Fprintf(&buf, "balance: %d", amount)
10
11line := ufmt.Sprintln("token", symbol, "transferred") // adds spaces + newline

API

 1// Format and return a string.
 2func Sprint(a ...any) string
 3func Sprintf(format string, a ...any) string
 4func Sprintln(a ...any) string
 5
 6// Format and write to an io.Writer.
 7func Fprint(w io.Writer, a ...any) (n int, err error)
 8func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
 9func Fprintln(w io.Writer, a ...any) (n int, err error)
10
11// Format and print to standard output.
12func Print(a ...any) (n int, err error)
13func Printf(format string, a ...any) (n int, err error)
14func Println(a ...any) (n int, err error)
15
16// Format and append to a byte slice.
17func Append(b []byte, a ...any) []byte
18func Appendf(b []byte, format string, a ...any) []byte
19func Appendln(b []byte, a ...any) []byte
20
21// Format and return an error.
22func Errorf(format string, args ...any) error

Supported verbs

Verb Meaning
%s String. Uses String() or Error() if implemented.
%d Integer (signed and unsigned, all widths).
%c Unicode character from rune/int code point.
%t Boolean: true or false.
%q Double-quoted, escaped string.
%x Hexadecimal (uint8 only).
%f / %F Decimal float; default precision 6.
%e / %E Scientific notation float; default precision 2.
%g / %G Float, compact representation.
%T Type name of the argument (basic types only).
%v Default representation appropriate for the value's type.
%% Literal %.

Width (%5s) and precision (%.2f) are supported for the relevant verbs.

Notes

  • Verb/type mismatches produce %!verb(type=value) strings, matching Go's fmt behaviour.
  • Missing or extra arguments panic.
  • Not supported: %b, %o, %U, %p, %+v, %#v, argument indexing, flags like -, +, #, 0.
  • Print* writes via the built-in print (stdout substitute) until os.Stdout is available.