fmtz

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 4 Imported by: 5

README

gopherz

ezpkg.io/fmtz

PkgGoDev GitHub License version

Package fmtz extends the standard library fmt with additional functions.

Installation

go get -u ezpkg.io/fmtz@v0.1.2

Examples

fmt.State

The stdlib fmt.State provides many functions that always return nil error. They have their counterparts as fmtz.State that eliminate the need of error handling. There is also fmtz.MustState that panics on error, which is useful when other types implement fmt.State that may return non-nil error.

type Code struct {
    Char   rune
    Number int
}

func (c Code) Format(s0 fmt.State, r rune) {
    s := fmtz.WrapState(s0)
    s.WriteRuneZ(c.Char)
    s.Print(c.Number)
}

func main() {
    c := Code{'Ω', 123}
    fmt.Printf("%v", c)
}
FormatMsgArgs

fmtz.FormatMsgArgs is a helper function that formats a message with arguments. It is useful for using in logging and error messages.

func validate(err error, msgAndArgs ...any) error {
    if err == nil {
		return nil
    }
	msg := fmtz.FormatMsgArgs(msgAndArgs...)
	return typez.If(msg == "", err, fmt.Errorf("%v: %w", msg, err))
}

func main() {
    someError := errors.New("something went wrong")
    err := validate(someError, "failed to do something foo=%v bar=%v", "10", "20")
    fmt.Println(err)
	// Output: failed to do something foo=10 bar=20: something went wrong
}

About ezpkg.io

As I work on various Go projects, I often find myself creating utility functions, extending existing packages, or developing packages to solve specific problems. Moving from one project to another, I usually have to copy or rewrite these solutions. So I created this repository to have all these utilities and packages in one place. Hopefully, you'll find them useful as well.

For more information, see the main repository.

Author

Oliver Nguyen  github

Documentation

Overview

Package fmtz provides utilities for working with formatted strings. It aims to extend the standard library fmt package with additional functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMsgArgs

func FormatMsgArgs(msgAndArgs []any) string

func FormatMsgArgsX

func FormatMsgArgsX(msgAndArgs []any) fmt.Formatter

Types

type MustState

type MustState struct {
	fmt.State
}

func WrapMustState

func WrapMustState(state fmt.State) MustState

func (MustState) Format added in v0.1.1

func (b MustState) Format(verb rune, value any)

func (MustState) FormatString added in v0.1.1

func (b MustState) FormatString(verb rune) string

func (MustState) Print

func (b MustState) Print(args ...any)

func (MustState) PrintBytes

func (b MustState) PrintBytes(p []byte)

func (MustState) Printf

func (b MustState) Printf(format string, args ...any)

func (MustState) Println

func (b MustState) Println(args ...any)

func (MustState) WriteByte

func (b MustState) WriteByte(c byte) error

func (MustState) WriteByteZ

func (b MustState) WriteByteZ(c byte)

func (MustState) WriteRune

func (b MustState) WriteRune(r rune) (int, error)

func (MustState) WriteRuneZ

func (b MustState) WriteRuneZ(r rune) int

func (MustState) WriteString

func (b MustState) WriteString(s string) (int, error)

func (MustState) WriteStringZ

func (b MustState) WriteStringZ(s string) int

func (MustState) WriteZ

func (b MustState) WriteZ(p []byte) int

type State

type State struct {
	fmt.State
}

func WrapState

func WrapState(state fmt.State) State

func (State) Format added in v0.1.1

func (b State) Format(verb rune, value any)

func (State) FormatString added in v0.1.1

func (b State) FormatString(verb rune) string

func (State) Print

func (b State) Print(args ...any)

func (State) PrintBytes

func (b State) PrintBytes(p []byte)

func (State) Printf

func (b State) Printf(format string, args ...any)

func (State) Println

func (b State) Println(args ...any)

func (State) WriteByte

func (b State) WriteByte(c byte) error

func (State) WriteByteZ

func (b State) WriteByteZ(c byte)

func (State) WriteRune

func (b State) WriteRune(r rune) (int, error)

func (State) WriteRuneZ

func (b State) WriteRuneZ(r rune) int

func (State) WriteString

func (b State) WriteString(s string) (int, error)

func (State) WriteStringZ

func (b State) WriteStringZ(s string) int

func (State) WriteZ

func (b State) WriteZ(p []byte) int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL