Monday, November 17, 2014

Golang Tip of the Year: fmt.Errorf

So, you want to create an error from the format string in golang.
That's easy, right?
import (
  "errors"
  "fmt"
)
err := errors.New(fmt.Sprintf("my %s error", "awesome"))
About 4000 golang projects  do errors.New(fmt.Sprintf(...

Don't do it, there is a better way:
import (
  "fmt"
)
err := fmt.Errorf("my %s error", "awesome")
You're welcome.

1 comment:

  1. Seems like all gophers removed this code, currently GitHub returns 682 projects contain this code.

    ReplyDelete