Please answer these questions before submitting your issue. Thanks!
go version)?go version go1.9 darwin/amd64
Yes
go env)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/alandaws"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/06/jjq25lws0cbcxv37ns0hb_mm0000gn/T/go-build733634811=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
Nothing, feature request
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
n/a
n/a
A common use of fmt.Errorf is to wrap a generic error returned from an upstream library so you can see where it occurred in your code:
x, err := something.DoThing()
if err != nil {
return nil, fmt.Errorf("Failed to do thing here: %s", err)
}
Concatenation in other variadic functions in fmt seems to be slightly faster than processing format strings, and in the case of wrapping errors, needs less syntax.
I could write:
x, err := something.DoThing()
if err != nil {
return nil, fmt.Error("Failed to do thing here: ", err)
}
The differences are only marginal, but it does fit with the pattern of a lot of the other functions in fmt, one that's just variadic and one that takes a format string.
Using Dave Cheney's errors.Wrap is possibly a better approach altogether: https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
Do people think a fmt.Error function is valuable?
The advantage of fmt.Error over errors.New seems very minor to me. Just change , to +.
You do have to also change err to err.Error(), but I agree that is equivalent for performance, and only slightly longer.
errors.New("Something: " + err.Error())
fmt.Errorf("Something: %s", err)
fmt.Error("Something: ", err)
It's already a little confusing that fmt.Errorf returns an error and log.Errorf prints a line of text to stderr. And .Error means other things in other APIs. Let's not compound this by adding fmt.Error here.
Note that you can already do errors.New(fmt.Sprint(...)) if you really want to avoid the format string.
Most helpful comment
The advantage of
fmt.Errorovererrors.Newseems very minor to me. Just change,to+.