Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: fmt: feature request: add Error, which corresponds to Errorf as Print corresponds to Printf #22848

Closed
brackendawson opened this issue Nov 22, 2017 · 4 comments

Comments

@brackendawson
Copy link

@brackendawson brackendawson commented Nov 22, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (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"

What did you do?

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.

What did you expect to see?

n/a

What did you see instead?

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?

@ianlancetaylor ianlancetaylor changed the title Feature request: Should we have fmt.Error() as well as fmt.Errorf()? fmt: feature request: add Error, which corresponds to Errorf as Print corresponds to Printf Nov 22, 2017
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 22, 2017

The advantage of fmt.Error over errors.New seems very minor to me. Just change , to +.

@brackendawson
Copy link
Author

@brackendawson brackendawson commented Nov 22, 2017

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)
@bradfitz bradfitz changed the title fmt: feature request: add Error, which corresponds to Errorf as Print corresponds to Printf proposal: fmt: feature request: add Error, which corresponds to Errorf as Print corresponds to Printf Nov 22, 2017
@gopherbot gopherbot added this to the Proposal milestone Nov 22, 2017
@gopherbot gopherbot added the Proposal label Nov 22, 2017
@rsc
Copy link
Contributor

@rsc rsc commented Nov 27, 2017

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.

@rsc
Copy link
Contributor

@rsc rsc commented Nov 27, 2017

Note that you can already do errors.New(fmt.Sprint(...)) if you really want to avoid the format string.

@rsc rsc closed this Nov 27, 2017
@golang golang locked and limited conversation to collaborators Nov 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.