Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(p/ufmt): add
ufmt.Errorf
(#1767)
# Description Added the `Errorf` function to `ufmt` based on `ufmt.Sprintf`. Previously, formatting errors involved a method like using `errors.New(ufmt.Sprintf(XXX))`, but this approach is not only cumbersome but also, as using `Errorf` is recommended in Go, I have newly incorporated it. ## Usage ### Simple Error This example demonstrates the process of using `Errorf` to format and print error messages. ```go package main import ( "gno.land/p/demo/ufmt" ) func foo() error { return fmt.Errorf("an error occurred in simpleFunction") } func main() { if err := foo(); err != nil { println("Error:", err) } } ``` ### With Panic Using panic is the recommended practice in [effective gno](<https://docs.gno.land/concepts/effective-gno/#embrace-panic>), In this example, format the error and then output the error message with `panic`. ```go package main import ( "gno.land/p/demo/ufmt" ) func foo() { msg := ufmt.Errorf("error: %s", "something went wrong") panic(msg) } ``` --------- Co-authored-by: Morgan <[email protected]>
- Loading branch information