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: Add constant error #17226

Closed
mkideal opened this issue Sep 25, 2016 · 8 comments
Closed

proposal: Add constant error #17226

mkideal opened this issue Sep 25, 2016 · 8 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@mkideal
Copy link

mkideal commented Sep 25, 2016

The origin of the problem

Here is an exmaple:

package mypkg

import "errors"

var (
    ErrNotFound = errors.New("not found")
)

func Find(s string) (int, error) {
    return 0, ErrNotFound
}

I need export the ErrNotFound, but ErrNotFound need to be guaranteed not to be modified.

Proposal

So I proposal add following code in errors package:

type Error string

func (e Error) Error() string { return string(e) }

Through doing this, I can define ErrNotFound like this:

const (
    ErrNotFound = errors.Error("not found")
)
@minux minux added the Proposal label Sep 25, 2016
@minux
Copy link
Member

minux commented Sep 25, 2016 via email

@mkideal
Copy link
Author

mkideal commented Sep 25, 2016

can assign a (untyped) string to errors.Error

Maybe some people need this.

@bradfitz
Copy link
Contributor

This isn't happening for Go 1, so kicking this into the Go2 abyss.

@bradfitz bradfitz added LanguageChange Suggested changes to the Go language v2 An incompatible library change labels Sep 25, 2016
@minux
Copy link
Member

minux commented Sep 25, 2016 via email

@bradfitz
Copy link
Contributor

We can discuss later.

@quentinmit quentinmit modified the milestone: Proposal Oct 4, 2016
@nizsheanez
Copy link

nizsheanez commented Nov 6, 2016

Faced use case with context keys:

ctx.Value("key") // cause allocation for convert string "key" to interface{}

Global variable do this allocation once:

var CONTEXT_KEY interface{} = "key" // for now using var instead of constx 
...
ctx.Value(CONTEXT_KEY) 

@bradfitz
Copy link
Contributor

bradfitz commented Nov 6, 2016

Do not use strings as context keys. See #17302 and the new golint rule.

@rsc rsc removed the Proposal label Nov 21, 2016
@rsc rsc modified the milestones: Unplanned, Proposal Nov 21, 2016
@rsc
Copy link
Contributor

rsc commented Jun 16, 2017

We used to use exactly the implementation of errors.Error given in the proposal. We moved away from it so that if two different packages used errors.New("msg") for different things, the two would not accidentally be equal.

I need export the ErrNotFound, but ErrNotFound need to be guaranteed not to be modified.

You probably don't need to guarantee it. The standard library exports many errors and makes no such guarantee, and programs work fine, because programs can mostly be trusted not to modify those errors (for example, they don't reassign io.EOF), and those programs that can't be trusted deserve to break badly anyway.

If you really needed to guarantee this, then instead of exporting the variable you could instead export a function that returns it:

var errNotFound = ...
func ErrNotFound() error { return errNotFound }

Then no one can modify the definition in use in your program.

@rsc rsc closed this as completed Jun 16, 2017
@golang golang locked and limited conversation to collaborators Jun 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

8 participants