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

add MustParse(), which returns an UUID or panics #26

Merged
merged 1 commit into from
Aug 28, 2018
Merged

add MustParse(), which returns an UUID or panics #26

merged 1 commit into from
Aug 28, 2018

Conversation

martinlindhe
Copy link
Contributor

@martinlindhe martinlindhe commented Nov 22, 2017

Same usability as with https://golang.org/pkg/regexp/#MustCompile

var someUUID = uuid.MustParse("f47ac10b-58cc-0372-8567-0e02b2c3d479")

panic(`uuid: Parse(` + s + `): ` + err.Error())
}
return uuid
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a generic Must function:

func Must(uuid UUID, err error) UUID {
if err != nil { panic('uuid: ' + err.Error) }
return uuid
}

And then you can say:

uuid.Must(uuid.Parse(s))
uuid.Must(uuid.FromBytes(b))

and so on.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Must function you mention is already in code https://github.com/google/uuid/blob/master/uuid.go#L108

Indeed uuid.Must(uuid.Parse(s)) could work just as well.

But I think it would help adding an example for this at least.

Do you want me to rework this PR to instead add a example function for uuid.Must(uuid.Parse(s)), or in a new PR?

With this patch I was just trying to follow some convention I imagined was around. It doesnt seem to be very enforced:

$ cd ~/dev/go-src
$ rg "func Must"
api/go1.txt
3374:pkg html/template, func Must(*Template, error) *Template
5523:pkg regexp, func MustCompile(string) *Regexp
5524:pkg regexp, func MustCompilePOSIX(string) *Regexp
28611:pkg syscall (windows-386), func MustLoadDLL(string) *DLL
29540:pkg syscall (windows-amd64), func MustLoadDLL(string) *DLL
30338:pkg text/template, func Must(*Template, error) *Template'
...

Copy link
Collaborator

@pborman pborman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have returned :-)

Okay, I buy your argument, but it will be in version 1.1 once that gets tagged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants