Skip to content
/ fmtz Public

Collection of packages and tools to make writing Go code easier.

License

Notifications You must be signed in to change notification settings

ezpkg/fmtz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gopherz

ezpkg.io/fmtz

PkgGoDev GitHub License version

Package fmtz extends the standard library fmt with additional functions.

Installation

go get -u ezpkg.io/[email protected]

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